-
Notifications
You must be signed in to change notification settings - Fork 2
/
swd_probe_app.c
3175 lines (2610 loc) · 96.1 KB
/
swd_probe_app.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "swd_probe_app.h"
#include "swd_probe_icons.h"
#include "jep106.h"
#include "adi.h"
static void render_callback(Canvas* const canvas, void* cb_ctx);
static bool swd_message_process(AppFSM* ctx);
static uint8_t swd_transfer(AppFSM* const ctx, bool ap, bool write, uint8_t a23, uint32_t* data);
static bool swd_execute_script(AppFSM* const ctx, const char* filename);
static const GpioPin* gpios[] = {
&gpio_ext_pc0,
&gpio_ext_pc1,
&gpio_ext_pc3,
&gpio_ext_pb2,
&gpio_ext_pb3,
&gpio_ext_pa4,
&gpio_ext_pa6,
&gpio_ext_pa7};
static const char* gpio_names[] = {"PC0", "PC1", "PC3", "PB2", "PB3", "PA4", "PA6", "PA7"};
/* bit set: clock, else data */
static const uint8_t gpio_direction_mask[6] =
{0b10101010, 0b01010101, 0b11001100, 0b00110011, 0b11110000, 0b00001111};
const NotificationSequence seq_c_minor = {
&message_note_c4,
&message_delay_100,
&message_sound_off,
&message_delay_10,
&message_note_ds4,
&message_delay_100,
&message_sound_off,
&message_delay_10,
&message_note_g4,
&message_delay_100,
&message_sound_off,
&message_delay_10,
&message_vibro_on,
&message_delay_50,
&message_vibro_off,
NULL,
};
const NotificationSequence seq_error = {
&message_vibro_on,
&message_delay_50,
&message_vibro_off,
&message_note_g4,
&message_delay_100,
&message_sound_off,
&message_delay_10,
&message_note_c4,
&message_delay_500,
&message_sound_off,
&message_delay_10,
NULL,
};
const NotificationSequence* seq_sounds[] = {&seq_c_minor, &seq_error};
static bool has_multiple_bits(uint8_t x) {
return (x & (x - 1)) != 0;
}
static uint8_t get_bit_num(uint8_t x) {
return (uint8_t)__builtin_ctz(x);
}
static const char* gpio_name(uint8_t mask) {
if(has_multiple_bits(mask)) {
return "Pxx";
}
uint8_t io = get_bit_num(mask);
if(io >= COUNT(gpio_names)) {
return "Pxx";
}
return gpio_names[io];
}
static void swd_configure_pins(AppFSM* const ctx, bool output) {
if(ctx->mode_page > ModePageFound && ctx->io_num_swc < 8 && ctx->io_num_swd < 8) {
furi_hal_gpio_init(
gpios[ctx->io_num_swc], GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
if(!output) {
furi_hal_gpio_init(
gpios[ctx->io_num_swd], GpioModeInput, GpioPullUp, GpioSpeedVeryHigh);
} else {
furi_hal_gpio_init(
gpios[ctx->io_num_swd], GpioModeOutputOpenDrain, GpioPullUp, GpioSpeedVeryHigh);
}
return;
}
for(int io = 0; io < 8; io++) {
uint8_t bitmask = 1 << io;
/* if neither candidate for SWC nor SWD then skip */
if(!(ctx->io_swc & bitmask) && !(ctx->io_swd & bitmask)) {
furi_hal_gpio_init(gpios[io], GpioModeInput, GpioPullUp, GpioSpeedVeryHigh);
continue;
}
if(ctx->current_mask & bitmask) {
/* set for clock */
furi_hal_gpio_init(gpios[io], GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
} else {
/* set for data */
if(!output) {
furi_hal_gpio_init(gpios[io], GpioModeInput, GpioPullUp, GpioSpeedVeryHigh);
} else {
furi_hal_gpio_init(
gpios[io], GpioModeOutputOpenDrain, GpioPullUp, GpioSpeedVeryHigh);
}
}
}
}
static void swd_set_clock(AppFSM* const ctx, const uint8_t level) {
if(ctx->mode_page > ModePageFound && ctx->io_num_swc < 8) {
furi_hal_gpio_write(gpios[ctx->io_num_swc], level);
return;
}
for(int io = 0; io < 8; io++) {
uint8_t bitmask = 1 << io;
/* if no candidate for SWC then skip */
if(!(ctx->io_swc & bitmask)) {
continue;
}
if(ctx->current_mask & bitmask) {
furi_hal_gpio_write(gpios[io], level);
}
}
}
static void swd_set_data(AppFSM* const ctx, const uint8_t level) {
if(ctx->mode_page > ModePageFound && ctx->io_num_swd < 8) {
furi_hal_gpio_write(gpios[ctx->io_num_swd], level);
return;
}
for(int io = 0; io < 8; io++) {
uint8_t bitmask = 1 << io;
/* if no candidate for SWD then skip */
if(!(ctx->io_swd & bitmask)) {
continue;
}
if(!(ctx->current_mask & bitmask)) {
furi_hal_gpio_write(gpios[io], level);
}
}
}
static uint8_t swd_get_data(AppFSM* const ctx) {
if(ctx->mode_page > ModePageFound && ctx->io_num_swd < 8) {
return furi_hal_gpio_read(gpios[ctx->io_num_swd]);
}
uint8_t bits = 0;
for(int io = 0; io < 8; io++) {
uint8_t bitmask = 1 << io;
/* if no candidate for SWD then skip */
if(!(ctx->io_swd & bitmask)) {
continue;
}
bits |= furi_hal_gpio_read(gpios[io]) ? bitmask : 0;
}
return bits;
}
static void swd_clock_delay(AppFSM* const ctx) {
if(ctx->swd_clock_delay) {
furi_delay_us(ctx->swd_clock_delay);
}
}
static void swd_write_bit(AppFSM* const ctx, bool level) {
swd_set_clock(ctx, 0);
swd_set_data(ctx, level);
swd_clock_delay(ctx);
swd_set_clock(ctx, 1);
swd_clock_delay(ctx);
swd_set_clock(ctx, 0);
}
static uint8_t swd_read_bit(AppFSM* const ctx) {
swd_set_clock(ctx, 1);
swd_clock_delay(ctx);
swd_set_clock(ctx, 0);
uint8_t bits = swd_get_data(ctx);
swd_clock_delay(ctx);
swd_set_clock(ctx, 1);
return bits;
}
/* send a byte or less LSB-first */
static void swd_write_byte(AppFSM* const ctx, const uint8_t data, size_t bits) {
for(size_t pos = 0; pos < bits; pos++) {
swd_write_bit(ctx, data & (1 << pos));
}
}
/* send a sequence of bytes LSB-first */
static void swd_write(AppFSM* const ctx, const uint8_t* data, size_t bits) {
size_t byte_pos = 0;
while(bits > 0) {
size_t remain = (bits > 8) ? 8 : bits;
swd_write_byte(ctx, data[byte_pos++], remain);
bits -= remain;
}
}
static uint8_t swd_transfer(AppFSM* const ctx, bool ap, bool write, uint8_t a23, uint32_t* data) {
//notification_message(ctx->notification, &sequence_set_blue_255);
//notification_message(ctx->notification, &sequence_reset_red);
swd_set_data(ctx, false);
swd_configure_pins(ctx, true);
uint32_t idle = 0;
swd_write(ctx, (uint8_t*)&idle, ctx->swd_idle_bits);
uint8_t request[] = {0};
request[0] |= 0x01; /* start bit*/
request[0] |= ap ? 0x02 : 0; /* APnDP */
request[0] |= write ? 0 : 0x04; /* operation */
request[0] |= (a23 & 0x01) ? 0x08 : 0; /* A[2:3] */
request[0] |= (a23 & 0x02) ? 0x10 : 0; /* A[2:3] */
request[0] |= 0x80; /* park bit */
request[0] |= __builtin_parity(request[0]) ? 0x20 : 0; /* parity */
swd_write(ctx, request, sizeof(request) * 8);
/* turnaround cycle */
swd_configure_pins(ctx, false);
uint8_t ack = 0;
/* receive 3 ACK bits */
for(int pos = 0; pos < 3; pos++) {
ack >>= 1;
ack |= swd_read_bit(ctx) ? 0x04 : 0;
}
/* force ABORT/CTRL to always work */
if(!ap && a23 == 0) {
ack = 1;
}
if(ack != 0x01) {
//notification_message(ctx->notification, &sequence_reset_blue);
//notification_message(ctx->notification, &sequence_set_red_255);
return ack;
}
if(write) {
swd_write_bit(ctx, 0);
swd_configure_pins(ctx, true);
/* send 32 WDATA bits */
for(int pos = 0; pos < 32; pos++) {
swd_write_bit(ctx, *data & (1 << pos));
}
/* send parity bit */
swd_write_bit(ctx, __builtin_parity(*data));
} else {
*data = 0;
/* receive 32 RDATA bits */
for(int pos = 0; pos < 32; pos++) {
*data >>= 1;
*data |= swd_read_bit(ctx) ? 0x80000000 : 0;
}
/* receive parity bit */
bool parity = swd_read_bit(ctx);
if(parity != __builtin_parity(*data)) {
//notification_message(ctx->notification, &sequence_reset_blue);
//notification_message(ctx->notification, &sequence_set_red_255);
return 8;
}
}
swd_set_data(ctx, false);
swd_configure_pins(ctx, true);
//notification_message(ctx->notification, &sequence_reset_blue);
return ack;
}
/* A line reset is achieved by holding the data signal HIGH for at least 50 clock cycles, followed by at least two idle cycles. */
static void swd_line_reset(AppFSM* const ctx) {
//notification_message(ctx->notification, &sequence_set_red_255);
for(int bitcount = 0; bitcount < 50; bitcount += 8) {
swd_write_byte(ctx, 0xFF, 8);
}
swd_write_byte(ctx, 0, 8);
ctx->dp_regs.select_ok = false;
//notification_message(ctx->notification, &sequence_reset_red);
}
static void swd_abort(AppFSM* const ctx) {
uint32_t dpidr;
/* first reset the line */
swd_line_reset(ctx);
swd_transfer(ctx, false, false, 0, &dpidr);
uint32_t abort = 0x0E;
swd_transfer(ctx, false, true, 0, &abort);
}
static void swd_abort_simple(AppFSM* const ctx) {
uint32_t abort = 0x0E;
swd_transfer(ctx, false, true, 0, &abort);
uint32_t dpidr;
if(swd_transfer(ctx, false, false, 0, &dpidr) != 1) {
swd_abort(ctx);
}
}
static uint8_t swd_select(AppFSM* const ctx, uint8_t ap_sel, uint8_t ap_bank, uint8_t dp_bank) {
uint32_t bank_reg = (ap_sel << 24) | ((ap_bank & 0x0F) << 4) | (dp_bank & 0x0F);
if(ctx->dp_regs.select_ok && bank_reg == ctx->dp_regs.select) {
return 1;
}
uint8_t ret = swd_transfer(ctx, false, true, REG_SELECT, &bank_reg);
if(ret != 1) {
ctx->dp_regs.select_ok = false;
DBG("failed: %d", ret);
return ret;
}
ctx->dp_regs.select = bank_reg;
ctx->dp_regs.select_ok = true;
return ret;
}
static uint8_t
swd_read_dpbank(AppFSM* const ctx, uint8_t dp_off, uint8_t dp_bank, uint32_t* data) {
uint8_t ret = 0;
/* select target bank */
if(dp_bank < 0x10) {
uint8_t ret = swd_select(ctx, 0, 0, dp_bank);
if(ret != 1) {
DBGS("swd_select failed");
return ret;
}
}
/* read data from it */
*data = 0;
ret = swd_transfer(ctx, false, false, dp_off, data);
if(ret != 1) {
DBG("failed: %d", ret);
return ret;
}
return ret;
}
static uint8_t
swd_write_dpbank(AppFSM* const ctx, uint8_t dp_off, uint8_t dp_bank, uint32_t* data) {
uint8_t ret = 0;
/* select target bank */
if(dp_bank < 0x10) {
ret = swd_select(ctx, 0, 0, dp_bank);
if(ret != 1) {
DBGS("swd_select failed");
return ret;
}
}
/* write it */
ret = swd_transfer(ctx, false, true, dp_off, data);
if(ret != 1) {
DBG("failed: %d", ret);
return ret;
}
return ret;
}
static uint8_t swd_read_ap(AppFSM* const ctx, uint8_t ap, uint8_t ap_off, uint32_t* data) {
/* select target bank */
uint8_t ret = swd_select(ctx, ap, (ap_off >> 4) & 0x0F, 0);
if(ret != 1) {
DBGS("swd_select failed");
return ret;
}
ret = swd_transfer(ctx, true, false, (ap_off >> 2) & 3, data);
*data = 0;
ret = swd_transfer(ctx, true, false, (ap_off >> 2) & 3, data);
if(ret != 1) {
DBG("failed: %d", ret);
return ret;
}
return ret;
}
static uint8_t swd_read_ap_single(AppFSM* const ctx, uint8_t ap, uint8_t ap_off, uint32_t* data) {
uint8_t ret = swd_select(ctx, ap, (ap_off >> 4) & 0x0F, 0);
if(ret != 1) {
DBGS("swd_select failed");
return ret;
}
*data = 0;
ret = swd_transfer(ctx, true, false, (ap_off >> 2) & 3, data);
if(ret != 1) {
DBG("failed: %d", ret);
return ret;
}
return ret;
}
static uint8_t swd_write_ap(AppFSM* const ctx, uint8_t ap, uint8_t ap_off, uint32_t data) {
uint8_t ret = swd_select(ctx, ap, (ap_off >> 4) & 0x0F, 0);
if(ret != 1) {
DBGS("swd_select failed");
return ret;
}
ret = swd_transfer(ctx, true, true, (ap_off >> 2) & 3, &data);
if(ret != 1) {
DBG("failed: %d", ret);
return ret;
}
return ret;
}
static uint8_t swd_write_memory(AppFSM* const ctx, uint8_t ap, uint32_t address, uint32_t data) {
uint8_t ret = 0;
uint32_t csw = 0x23000002;
ret |= swd_write_ap(ctx, ap, MEMAP_CSW, csw);
ret |= swd_write_ap(ctx, ap, MEMAP_TAR, address);
ret |= swd_write_ap(ctx, ap, MEMAP_DRW, data);
DBG("write 0x%08lX to 0x%08lX", data, address);
if(ret != 1) {
swd_abort(ctx);
}
return ret;
}
uint8_t swd_read_memory(AppFSM* const ctx, uint8_t ap, uint32_t address, uint32_t* data) {
uint8_t ret = 0;
uint32_t csw = 0x23000002;
ret |= swd_write_ap(ctx, ap, MEMAP_CSW, csw);
ret |= swd_write_ap(ctx, ap, MEMAP_TAR, address);
ret |= swd_read_ap(ctx, ap, MEMAP_DRW, data);
if(ret != 1) {
DBG("read from 0x%08lX failed", address);
swd_abort(ctx);
} else {
DBG("read 0x%08lX from 0x%08lX", *data, address);
}
return ret;
}
static uint8_t swd_read_memory_block(
AppFSM* const ctx,
uint8_t ap,
uint32_t address,
uint8_t* buf,
uint32_t len) {
uint8_t ret = 0;
uint32_t data = 0;
uint32_t csw = 0x23000012;
ret |= swd_write_ap(ctx, ap, MEMAP_CSW, csw);
ret |= swd_write_ap(ctx, ap, MEMAP_TAR, address);
ret |= swd_read_ap_single(ctx, ap, MEMAP_DRW, &data);
for(size_t pos = 0; pos < len; pos += 4) {
data = 0xDEADBEEF;
ret |= swd_read_ap_single(ctx, ap, MEMAP_DRW, &data);
DBG("read %lX", data);
memcpy(&buf[pos], &data, 4);
if(ret != 1) {
swd_abort(ctx);
return ret;
}
}
return ret;
}
static uint32_t swd_detect(AppFSM* const ctx) {
swd_set_data(ctx, false);
swd_configure_pins(ctx, true);
uint8_t data[] = {0xA5};
swd_write(ctx, data, sizeof(data) * 8);
/* turnaround cycle */
swd_configure_pins(ctx, false);
uint8_t ack_bits[3];
uint8_t rdata[32];
/* receive 3 ACK bits */
for(int pos = 0; pos < 3; pos++) {
ack_bits[pos] = swd_read_bit(ctx);
}
/* receive 32 RDATA bits */
for(int pos = 0; pos < 32; pos++) {
rdata[pos] = swd_read_bit(ctx);
}
/* receive parity bit */
uint8_t parity = swd_read_bit(ctx);
for(int io = 0; io < 8; io++) {
uint8_t bitmask = 1 << io;
/* skip if it's a clock */
if(ctx->current_mask & bitmask) {
continue;
}
uint8_t ack = 0;
for(int pos = 0; pos < 3; pos++) {
ack >>= 1;
ack |= (ack_bits[pos] & bitmask) ? 4 : 0;
}
uint32_t dpidr = 0;
for(int pos = 0; pos < 32; pos++) {
dpidr >>= 1;
dpidr |= (rdata[pos] & bitmask) ? 0x80000000 : 0;
}
if(ack == 1 && dpidr != 0 && dpidr != 0xFFFFFFFF) {
bool received_parity = (parity & bitmask);
if(__builtin_parity(dpidr) == received_parity) {
ctx->dp_regs.dpidr = dpidr;
ctx->dp_regs.dpidr_ok = true;
ctx->detected = true;
ctx->io_swd = bitmask;
ctx->io_swc &= ctx->current_mask;
LOG("swd_detect: data: %08lX, io_swd %02X, io_swc %02X",
dpidr,
ctx->io_swd,
ctx->io_swc);
if(!has_multiple_bits(ctx->io_swc)) {
ctx->io_num_swd = get_bit_num(ctx->io_swd);
ctx->io_num_swc = get_bit_num(ctx->io_swc);
}
}
}
}
swd_set_data(ctx, false);
swd_configure_pins(ctx, true);
return 0;
}
static void swd_scan(AppFSM* const ctx) {
/* To switch SWJ-DP from JTAG to SWD operation:
1. Send at least 50 SWCLKTCK cycles with SWDIOTMS HIGH. This ensures that the current interface is in its reset state. The JTAG interface only detects the 16-bit JTAG-to-SWD sequence starting from the Test-Logic-Reset state.
2. Send the 16-bit JTAG-to-SWD select sequence 0x79e7 on SWDIOTMS.
3. Send at least 50 SWCLKTCK cycles with SWDIOTMS HIGH. This ensures that if SWJ-DP was already in SWD operation before sending the select sequence, the SWD interface enters line reset state.
*/
swd_configure_pins(ctx, true);
/* reset JTAG interface */
for(int bitcount = 0; bitcount < 50; bitcount += 8) {
swd_write_byte(ctx, 0xFF, 8);
}
/* Send the 16-bit JTAG-to-SWD select sequence */
swd_write_byte(ctx, 0x9E, 8);
swd_write_byte(ctx, 0xE7, 8);
/* resynchronize SWD */
swd_line_reset(ctx);
swd_detect(ctx);
}
static bool swd_ensure_powerup(AppFSM* const ctx) {
bool ret = true;
if(!(ctx->dp_regs.ctrlstat & (CSYSPWRUPREQ | CDBGPWRUPREQ))) {
DBGS("no (CSYSPWRUPREQ | CDBGPWRUPREQ)");
/* fetch current CTRL/STAT */
DBGS(" - Fetch CTRL/STAT");
ctx->dp_regs.ctrlstat_ok =
swd_read_dpbank(ctx, REG_CTRLSTAT, REG_CTRLSTAT_BANK, &ctx->dp_regs.ctrlstat) == 1;
DBG(" %08lX %s", ctx->dp_regs.ctrlstat, ctx->dp_regs.ctrlstat_ok ? "OK" : "FAIL");
/* enable requests */
ctx->dp_regs.ctrlstat |= (CSYSPWRUPREQ | CDBGPWRUPREQ);
swd_write_dpbank(ctx, REG_CTRLSTAT, REG_CTRLSTAT_BANK, &ctx->dp_regs.ctrlstat);
ret = false;
}
if(!(ctx->dp_regs.ctrlstat & CDBGPWRUPACK)) {
DBGS("no CDBGPWRUPACK");
/* fetch current CTRL/STAT */
swd_read_dpbank(ctx, REG_CTRLSTAT, REG_CTRLSTAT_BANK, &ctx->dp_regs.ctrlstat);
ret = false;
}
if(!ret) {
DBGS(" - Fetch CTRL/STAT");
ctx->dp_regs.ctrlstat_ok =
swd_read_dpbank(ctx, REG_CTRLSTAT, REG_CTRLSTAT_BANK, &ctx->dp_regs.ctrlstat) == 1;
DBG(" %08lX %s", ctx->dp_regs.ctrlstat, ctx->dp_regs.ctrlstat_ok ? "OK" : "FAIL");
}
return ret;
}
static void swd_apscan_reset(AppFSM* const ctx) {
for(size_t reset_ap = 0; reset_ap < COUNT(ctx->apidr_info); reset_ap++) {
ctx->apidr_info[reset_ap].tested = false;
}
}
static bool swd_apscan_test(AppFSM* const ctx, uint32_t ap) {
furi_assert(ctx);
furi_assert(ap < sizeof(ctx->apidr_info));
bool ret = true;
ctx->apidr_info[ap].tested = true;
uint32_t data = 0;
if(swd_read_ap(ctx, ap, AP_IDR, &data) != 1) {
swd_abort(ctx);
return false;
}
if(data == 0) {
return false;
}
DBG("AP%lu detected", ap);
ctx->apidr_info[ap].ok = true;
ctx->apidr_info[ap].revision = (data >> 24) & 0x0F;
ctx->apidr_info[ap].designer = (data >> 17) & 0x3FF;
ctx->apidr_info[ap].class = (data >> 13) & 0x0F;
ctx->apidr_info[ap].variant = (data >> 4) & 0x0F;
ctx->apidr_info[ap].type = (data >> 0) & 0x0F;
if(swd_read_ap(ctx, ap, AP_BASE, &ctx->apidr_info[ap].base) != 1) {
swd_abort(ctx);
ret = false;
}
return ret;
}
/************************** script helpers **************************/
static void swd_script_log(ScriptContext* ctx, FuriLogLevel level, const char* format, ...) {
bool commandline = false;
ScriptContext* cur = ctx;
char buffer[256];
va_list argp;
va_start(argp, format);
do {
if(cur == ctx->app->commandline) {
commandline = true;
}
cur = cur->parent;
} while(cur);
if(commandline) {
const char* prefix = "";
switch(level) {
case FuriLogLevelWarn:
prefix = "Warning: ";
break;
case FuriLogLevelError:
prefix = "ERROR: ";
break;
default:
break;
}
strcpy(buffer, prefix);
size_t pos = strlen(buffer);
vsnprintf(&buffer[pos], sizeof(buffer) - pos - 2, format, argp);
strcat(buffer, "\n");
if(!usb_uart_tx_data(ctx->app->uart, (uint8_t*)buffer, strlen(buffer))) {
DBGS("Sending via USB failed");
}
} else {
LOG(buffer);
}
va_end(argp);
}
/* read characters until newline was read */
static bool swd_script_seek_newline(ScriptContext* ctx) {
while(true) {
uint8_t ch = 0;
if(ctx->script_file) {
if(storage_file_read(ctx->script_file, &ch, 1) != 1) {
return false;
}
} else {
ch = ctx->line_data[ctx->line_pos];
if(ch == 0) {
return false;
}
ctx->line_pos++;
}
if(ch == '\n') {
return true;
}
}
}
/* read whitespaces until the next character is read.
returns false if EOF or newline was read */
static bool swd_script_skip_whitespace(ScriptContext* ctx) {
while(true) {
uint8_t ch = 0;
uint64_t start_pos = 0;
if(ctx->script_file) {
start_pos = storage_file_tell(ctx->script_file);
if(storage_file_read(ctx->script_file, &ch, 1) != 1) {
return false;
}
} else {
start_pos = ctx->line_pos;
ch = ctx->line_data[ctx->line_pos];
if(ch == 0) {
return false;
}
ctx->line_pos++;
}
if(ch == '\n') {
return false;
}
if(ch != ' ') {
if(ctx->script_file) {
storage_file_seek(ctx->script_file, start_pos, true);
} else {
ctx->line_pos = start_pos;
}
return true;
}
}
}
static bool swd_script_get_string(ScriptContext* ctx, char* str, size_t max_length) {
bool quot = false;
size_t pos = 0;
str[pos] = '\000';
while(true) {
char ch = 0;
uint64_t start_pos = 0;
if(ctx->script_file) {
start_pos = storage_file_tell(ctx->script_file);
if(storage_file_read(ctx->script_file, &ch, 1) != 1) {
DBGS("end reached");
return false;
}
} else {
start_pos = ctx->line_pos;
ch = ctx->line_data[ctx->line_pos];
if(ch == 0) {
DBGS("end reached");
return false;
}
ctx->line_pos++;
}
if(ch == '"') {
quot = !quot;
continue;
}
if(!quot) {
if(ch == ' ') {
break;
}
if(ch == '\r' || ch == '\n') {
if(ctx->script_file) {
storage_file_seek(ctx->script_file, start_pos, true);
} else {
ctx->line_pos = start_pos;
}
break;
}
}
if(pos + 2 > max_length) {
DBGS("too long");
return false;
}
str[pos++] = ch;
str[pos] = '\000';
}
DBG("got '%s'", str);
return true;
}
static bool swd_script_get_number(ScriptContext* ctx, uint32_t* number) {
char str[16];
if(!swd_script_get_string(ctx, str, sizeof(str))) {
DBGS("could not get string");
return false;
}
DBG("got '%s'", str);
size_t pos = 0;
*number = 0;
/* hex number? */
if(!strncmp(str, "0x", 2)) {
pos += 2;
while(str[pos]) {
uint8_t ch = str[pos++];
uint8_t ch_num = ch - '0';
uint8_t ch_hex = (ch & ~0x20) - 'A';
*number <<= 4;
if(ch_num <= 10) {
*number += ch_num;
} else if(ch_hex <= 5) {
*number += 10 + ch_hex;
} else {
return false;
}
}
} else {
while(str[pos]) {
uint8_t ch = str[pos++];
uint8_t ch_num = ch - '0';
*number *= 10;
if(ch_num < 10) {
*number += ch_num;
} else {
return false;
}
}
}
return true;
}
static void swd_script_gui_refresh(ScriptContext* ctx) {
if(furi_message_queue_get_count(ctx->app->event_queue) > 0) {
swd_message_process(ctx->app);
}
if(!ctx->status_ignore) {
DBG("Status: %s", ctx->app->state_string);
view_port_update(ctx->app->view_port);
}
}
/************************** script functions **************************/
static bool swd_scriptfunc_comment(ScriptContext* ctx) {
DBGS("comment");
swd_script_seek_newline(ctx);
return true;
}
static bool swd_scriptfunc_label(ScriptContext* ctx) {
char label[256];
DBGS("label");
swd_script_skip_whitespace(ctx);
if(!swd_script_get_string(ctx, label, sizeof(label))) {
swd_script_log(ctx, FuriLogLevelError, "failed to parse label");
return false;
}
if(!strcmp(label, ctx->goto_label)) {
ctx->goto_active = false;
DBG("matches '%s'", ctx->goto_label);
}
swd_script_seek_newline(ctx);
return true;
}
static bool swd_scriptfunc_goto(ScriptContext* ctx) {
DBGS("goto");
swd_script_skip_whitespace(ctx);
if(!swd_script_get_string(ctx, ctx->goto_label, sizeof(ctx->goto_label))) {
swd_script_log(ctx, FuriLogLevelError, "failed to parse target label");
return false;
}
/* start from beginning and rerun starting from label */
ctx->goto_active = true;
ctx->restart = true;
swd_script_seek_newline(ctx);
return true;
}
static bool swd_scriptfunc_call(ScriptContext* ctx) {
DBGS("call");
swd_script_skip_whitespace(ctx);
/* fetch previous file directory */
char filename[MAX_FILE_LENGTH];
strncpy(filename, ctx->filename, sizeof(filename));
char* path = strrchr(filename, '/');
path[1] = '\000';
/* append filename */
if(!swd_script_get_string(ctx, &path[1], sizeof(filename) - strlen(path))) {
swd_script_log(ctx, FuriLogLevelError, "failed to parse filename");
return false;
}
swd_script_seek_newline(ctx);
/* append extension */
if(strlen(filename) + 5 >= sizeof(filename)) {
swd_script_log(ctx, FuriLogLevelError, "name too long");
return false;
}
strcat(filename, ".swd");
bool ret = swd_execute_script(ctx->app, filename);
if(!ret) {
swd_script_log(ctx, FuriLogLevelError, "failed to exec '%s'", filename);
return false;
}
return true;
}
static bool swd_scriptfunc_status(ScriptContext* ctx) {
uint32_t status = 1;
DBGS("status");
swd_script_skip_whitespace(ctx);
swd_script_get_number(ctx, &status);
ctx->status_ignore = (status == 0);
swd_script_seek_newline(ctx);
return true;
}
static bool swd_scriptfunc_errors(ScriptContext* ctx) {
char type[32];
DBGS("errors");
swd_script_skip_whitespace(ctx);
if(!swd_script_get_string(ctx, type, sizeof(type))) {
swd_script_log(ctx, FuriLogLevelError, "failed to parse");