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
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
238
239
246
247
248
249
250
251
252
253
257
258
259
260
261
262
263
264
268
269
270
271
272
273
274
275
285
286
296
297
298
299
300
301
302
303
304
305
306
307
314
315
316
323
324
325
332
333
334
341
342
343
350
351
352
359
360
361
368
369
370
377
378
379
383
384
385
391
392
393
399
400
401
407
408
409
415
416
417
423
424
425
431
432
433
434
435
436
437
438
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
466
467
468
469
470
476
477
478
479
480
481
482
486
487
488
489
490
491
503
504
505
506
507
508
509
510
511
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
566
567
568
569
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
603
604
605
606
607
608
612
613
614
615
616
617
618
619
624
625
626
627
628
632
633
634
635
636
637
642
643
648
649
653
657
658
663
664
665
669
670
672
673
674
675
676
677
678
679
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
711
712
713
714
715
716
717
718
723
724
725
726
727
728
732
733
734
735
736
740
741
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
785
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
826
831
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
871
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
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
974
975
976
977
978
979
980
981
985
986
987
988
989
994
995
999
1000
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1016
1017
1018
1020
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1066
1067
1068
1069
1070
1071
1072
1073
1077
1078
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1133
1134
1135
1136
1140
1141
1142
1143
1144
1149
1150
1154
1155
1157
1158
1159
1160
1161
1162
1163
1164
1168
1169
1170
1172
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1214
1215
1216
1220
1221
1222
1223
1224
1225
1226
1227
1231
1232
1240
1241
1242
1243
1244
1245
1257
1258
1259
1271
1272
1273
1285
1286
1287
1299
1300
1301
1309
1310
1311
1319
1320
1321
1329
1330
1331
1335
1336
1337
1345
1346
1350
1351
1352
1353
1357
1358
1364
1365
1366
1367
1368
1374
1375
1376
1377
1378
1379
1380
1381
1385
1386
1387
1388
1389
1390
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1444
1445
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1469
1470
1476
1477
1478
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1505
1506
1509
1515
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1783
1784
1785
1786
1787
1788
1789
1790
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1857
1858
1859
1862
1863
1864
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1892
1893
1894
1898
1899
1903
1904
1905
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1968
1969
1970
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2108
2109
2110
2111
2112
2113
2114
2115
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2132
2133
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2213
2214
2215
2216
2217
2218
2219
2220
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2248
2249
2250
2251
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2293
2294
2295
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2333
2334
2335
2336
2337
2338
2342
2343
2344
2345
2346
2347
2348
2352
2353
2354
2355
2356
2357
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2417
2418
2419
2420
2421
2422
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2487
2491
2492
2493
2494
2495
2499
2500
2504
2505
2506
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2543
2544
2545
2546
2556
2557
2558
2559
2560
2561
2562
2563
2567
2568
2569
2570
2580
2581
2582
2583
2584
2585
2586
2587
2591
2592
2593
2594
2604
2605
2606
2607
2608
2609
2610
2614
2615
2616
2617
2626
2627
2628
2629
2630
2631
2632
2633
2634
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2799
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2901
2902
2903
2904
2905
2906
2907
2908
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2928
2929
2930
2931
2932
2933
2934
2938
2939
2944
2948
2953
2957
2958
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2991
2992
2993
2994
2995
2996
2997
2998
2999
3003
3004
3007
3008
3009
3010
3011
3012
3013
3015
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3096
3097
3098
3099
3100
3101
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3145
3146
3147
3151
3152
3153
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3213
3214
3215
3216
3217
3218
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3243
3244
3249
3250
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3325
3326
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3344
3345
3346
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3371
3374
3377
3380
3384
3385
3386
3387
3393
3394
3395
3396
3409
3410
3411
3412
3413
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3430
3433
3436
3439
3441
3442
3443
3444
3445
3446
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3467
3468
3469
3470
3471
3472
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3496
3497
3498
3502
3503
3504
3505
3508
3511
3514
3517
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3577
3580
3583
3586
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3694
3700
3701
3702
3703
3704
3705
3706
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3771
3772
3773
3774
3775
3776
3777
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3890
3891
3892
3893
3894
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3921
3922
3926
3927
3928
3929
3930
3934
3935
3936
3940
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3972
3973
3974
3980
3981
3982
3983
3984
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
4003
4004
4005
4006
4007
4008
4009
4013
4014
4018
4019
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4076
4077
4078
4079
4080
4081
4082
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4105
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4202
4208
4224
4229
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4338
4339
4340
4346
4347
4349
4350
4351
4352
4353
4359
4360
4361
4362
4363
4365
4366
4367
4368
4369
4370
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4449
4450
4451
4455
4456
4457
4462
4463
4464
4468
4469
4470
4471
4472
4473
4477
4478
4495
4496
4497
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4544
4545
4546
4547
4548
4549
4551
4554
4555
4556
4557
4558
4562
4563
4567
4568
4569
4570
4571
4572
4573
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4597
4598
4599
4600
4601
4602
4603
4604
4605
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4642
4643
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4678
4679
4680
4681
4682
4683
4684
4685
4686
4688
4691
4692
4693
4694
4695
4699
4700
4704
4705
4706
4707
4708
4709
4710
4711
4712
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4838
4839
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4870
4871
4880
4881
4882
4883
4887
4888
4889
4890
4891
4892
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4919
4920
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4941
4942
4943
4947
4948
4949
4953
4954
4955
4956
4957
4962
4963
4964
4965
4966
4967
4970
4971
4972
4973
4974
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5036
5040
5041
5042
5043
5047
5048
5049
5050
5063
5064
5065
5066
5067
5068
5072
5073
5074
5078
5079
5090
5091
5092
5093
5094
5095
5096
5101
5102
5103
5104
5105
5106
5107
5108
5109
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5208
5209
5210
5211
5212
5213
5214
5225
5226
5230
5231
5232
5233
5234
5248
5249
5250
5251
5252
5253
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5286
5287
5288
5289
5299
5305
5306
5307
5308
5312
5313
5314
5315
5316
5317
5318
5319
5323
5324
5325
5326
5327
5328
5329
5330
5334
5335
5336
5337
5338
5339
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5383
5384
5385
5389
5390
5395
5396
5397
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5468
5469
5470
5471
5472
5473
5474
5478
5479
5483
5484
5485
5486
5487
5488
5489
5490
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5524
5525
5526
5527
5528
5529
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5610
5611
5612
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5826
5827
5828
5829
5830
5831
5836
5837
5838
5839
5840
5841
5842
5850
5851
5857
5874
5875
5876
5877
5881
5882
5883
5884
5885
5886
5887
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
6002
6003
6004
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6114
6115
6116
6117
6118
6119
6120
6121
6122
6126
6127
6131
6132
6133
6137
6138
6139
6144
6145
6146
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6227
6228
6229
6230
6231
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6272
6273
6274
6275
6281
6282
6283
6287
6288
6289
6290
6291
6292
6293
6294
6295
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6395
6396
6397
6398
6402
6403
6409
6410
6411
6412
6413
6414
6418
6419
6420
6421
6422
6423
6424
6425
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6474
6475
6476
6480
6481
6482
6483
6484
6485
6486
6487
6488
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6527
6528
6529
6530
6531
6532
6533
6534
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6570
6571
6572
6573
6574
6575
6576
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6651
6652
6653
6654
6658
6659
6660
6661
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6880
6886
6892
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6999
7001
7003
7005
7007
7009
7011
7013
7015
7017
7018
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <helper/align.h>
#include <helper/nvp.h>
#include <helper/time_support.h>
#include <jtag/jtag.h>
#include <flash/nor/core.h>
#include "target.h"
#include "target_type.h"
#include "target_request.h"
#include "breakpoints.h"
#include "register.h"
#include "trace.h"
#include "image.h"
#include "rtos/rtos.h"
#include "transport/transport.h"
#include "arm_cti.h"
#include "smp.h"
#include "semihosting_common.h"
17 includes
#define DEFAULT_HALT_TIMEOUT 5000
static int target_read_buffer_default(struct target *target, target_addr_t address,
uint32_t count, uint8_t *buffer);
static int target_write_buffer_default(struct target *target, target_addr_t address,
uint32_t count, const uint8_t *buffer);
static int target_register_user_commands(struct command_context *cmd_ctx);
static int target_get_gdb_fileio_info_default(struct target *target,
struct gdb_fileio_info *fileio_info);
static int target_gdb_fileio_end_default(struct target *target, int retcode,
int fileio_errno, bool ctrl_c);
static struct target_type *target_types[] = {
&arm7tdmi_target,
&arm9tdmi_target,
&arm920t_target,
&arm720t_target,
&arm966e_target,
&arm946e_target,
&arm926ejs_target,
&fa526_target,
&feroceon_target,
&dragonite_target,
&xscale_target,
&xtensa_chip_target,
&cortexm_target,
&cortexa_target,
&cortexr4_target,
&arm11_target,
&ls1_sap_target,
&mips_m4k_target,
&avr_target,
&dsp563xx_target,
&dsp5680xx_target,
&testee_target,
&avr32_ap7k_target,
&hla_target,
&esp32_target,
&esp32s2_target,
&esp32s3_target,
&or1k_target,
&quark_x10xx_target,
&quark_d20xx_target,
&stm8_target,
&riscv_target,
&mem_ap_target,
&esirisc_target,
&arcv2_target,
&aarch64_target,
&armv8r_target,
&mips_mips64_target,
NULL,
...};
struct target *all_targets;
static struct target_event_callback *target_event_callbacks;
static struct target_timer_callback *target_timer_callbacks;
static int64_t target_timer_next_event_value;
static LIST_HEAD(target_reset_callback_list);
static LIST_HEAD(target_trace_callback_list);
static const int polling_interval = TARGET_DEFAULT_POLLING_INTERVAL;
static LIST_HEAD(empty_smp_targets);
enum nvp_assert {
NVP_DEASSERT,
NVP_ASSERT,
...};
static const struct nvp nvp_assert[] = {
{ .name = "assert", NVP_ASSERT },
{ .name = "deassert", NVP_DEASSERT },
{ .name = "T", NVP_ASSERT },
{ .name = "F", NVP_DEASSERT },
{ .name = "t", NVP_ASSERT },
{ .name = "f", NVP_DEASSERT },
{ .name = NULL, .value = -1 }
...};
static const struct nvp nvp_error_target[] = {
{ .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
{ .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
{ .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
{ .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
{ .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
{ .value = ERROR_TARGET_UNALIGNED_ACCESS, .name = "err-unaligned-access" },
{ .value = ERROR_TARGET_DATA_ABORT, .name = "err-data-abort" },
{ .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE, .name = "err-resource-not-available" },
{ .value = ERROR_TARGET_TRANSLATION_FAULT, .name = "err-translation-fault" },
{ .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
{ .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
{ .value = -1, .name = NULL }
...};
static const char *target_strerror_safe(int err)
{
const struct nvp *n;
n = nvp_value2name(nvp_error_target, err);
if (!n->name)
return "unknown";
else
return n->name;
}{ ... }
static const struct jim_nvp nvp_target_event[] = {
{ .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
{ .value = TARGET_EVENT_HALTED, .name = "halted" },
{ .value = TARGET_EVENT_RESUMED, .name = "resumed" },
{ .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
{ .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
{ .value = TARGET_EVENT_STEP_START, .name = "step-start" },
{ .value = TARGET_EVENT_STEP_END, .name = "step-end" },
{ .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
{ .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
{ .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
{ .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
{ .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
{ .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
{ .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
{ .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
{ .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
{ .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
{ .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
{ .value = TARGET_EVENT_EXAMINE_FAIL, .name = "examine-fail" },
{ .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
{ .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
{ .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
{ .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
{ .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
{ .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
{ .value = TARGET_EVENT_GDB_FLASH_WRITE_END, .name = "gdb-flash-write-end" },
{ .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
{ .value = TARGET_EVENT_GDB_FLASH_ERASE_END, .name = "gdb-flash-erase-end" },
{ .value = TARGET_EVENT_TRACE_CONFIG, .name = "trace-config" },
{ .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X100, .name = "semihosting-user-cmd-0x100" },
{ .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X101, .name = "semihosting-user-cmd-0x101" },
{ .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X102, .name = "semihosting-user-cmd-0x102" },
{ .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X103, .name = "semihosting-user-cmd-0x103" },
{ .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X104, .name = "semihosting-user-cmd-0x104" },
{ .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X105, .name = "semihosting-user-cmd-0x105" },
{ .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X106, .name = "semihosting-user-cmd-0x106" },
{ .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X107, .name = "semihosting-user-cmd-0x107" },
{ .name = NULL, .value = -1 }
...};
static const struct nvp nvp_target_state[] = {
{ .name = "unknown", .value = TARGET_UNKNOWN },
{ .name = "running", .value = TARGET_RUNNING },
{ .name = "halted", .value = TARGET_HALTED },
{ .name = "reset", .value = TARGET_RESET },
{ .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
{ .name = NULL, .value = -1 },
...};
static const struct nvp nvp_target_debug_reason[] = {
{ .name = "debug-request", .value = DBG_REASON_DBGRQ },
{ .name = "breakpoint", .value = DBG_REASON_BREAKPOINT },
{ .name = "watchpoint", .value = DBG_REASON_WATCHPOINT },
{ .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
{ .name = "single-step", .value = DBG_REASON_SINGLESTEP },
{ .name = "target-not-halted", .value = DBG_REASON_NOTHALTED },
{ .name = "program-exit", .value = DBG_REASON_EXIT },
{ .name = "exception-catch", .value = DBG_REASON_EXC_CATCH },
{ .name = "undefined", .value = DBG_REASON_UNDEFINED },
{ .name = NULL, .value = -1 },
...};
static const struct jim_nvp nvp_target_endian[] = {
{ .name = "big", .value = TARGET_BIG_ENDIAN },
{ .name = "little", .value = TARGET_LITTLE_ENDIAN },
{ .name = "be", .value = TARGET_BIG_ENDIAN },
{ .name = "le", .value = TARGET_LITTLE_ENDIAN },
{ .name = NULL, .value = -1 },
...};
static const struct nvp nvp_reset_modes[] = {
{ .name = "unknown", .value = RESET_UNKNOWN },
{ .name = "run", .value = RESET_RUN },
{ .name = "halt", .value = RESET_HALT },
{ .name = "init", .value = RESET_INIT },
{ .name = NULL, .value = -1 },
...};
const char *debug_reason_name(const struct target *t)
{
const char *cp;
cp = nvp_value2name(nvp_target_debug_reason,
t->debug_reason)->name;
if (!cp) {
LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
cp = "(*BUG*unknown*BUG*)";
}if (!cp) { ... }
return cp;
}{ ... }
const char *target_state_name(const struct target *t)
{
const char *cp;
cp = nvp_value2name(nvp_target_state, t->state)->name;
if (!cp) {
LOG_ERROR("Invalid target state: %d", (int)(t->state));
cp = "(*BUG*unknown*BUG*)";
}if (!cp) { ... }
if (!target_was_examined(t) && t->defer_examine)
cp = "examine deferred";
return cp;
}{ ... }
const char *target_event_name(enum target_event event)
{
const char *cp;
cp = jim_nvp_value2name_simple(nvp_target_event, event)->name;
if (!cp) {
LOG_ERROR("Invalid target event: %d", (int)(event));
cp = "(*BUG*unknown*BUG*)";
}if (!cp) { ... }
return cp;
}{ ... }
const char *target_reset_mode_name(enum target_reset_mode reset_mode)
{
const char *cp;
cp = nvp_value2name(nvp_reset_modes, reset_mode)->name;
if (!cp) {
LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode));
cp = "(*BUG*unknown*BUG*)";
}if (!cp) { ... }
return cp;
}{ ... }
static void append_to_list_all_targets(struct target *target)
{
struct target **t = &all_targets;
while (*t)
t = &((*t)->next);
*t = target;
}{ ... }
uint64_t target_buffer_get_u64(struct target *target, const uint8_t *buffer)
{
if (target->endianness == TARGET_LITTLE_ENDIAN)
return le_to_h_u64(buffer);
else
return be_to_h_u64(buffer);
}{ ... }
uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
{
if (target->endianness == TARGET_LITTLE_ENDIAN)
return le_to_h_u32(buffer);
else
return be_to_h_u32(buffer);
}{ ... }
uint32_t target_buffer_get_u24(struct target *target, const uint8_t *buffer)
{
if (target->endianness == TARGET_LITTLE_ENDIAN)
return le_to_h_u24(buffer);
else
return be_to_h_u24(buffer);
}{ ... }
uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
{
if (target->endianness == TARGET_LITTLE_ENDIAN)
return le_to_h_u16(buffer);
else
return be_to_h_u16(buffer);
}{ ... }
void target_buffer_set_u64(struct target *target, uint8_t *buffer, uint64_t value)
{
if (target->endianness == TARGET_LITTLE_ENDIAN)
h_u64_to_le(buffer, value);
else
h_u64_to_be(buffer, value);
}{ ... }
void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
{
if (target->endianness == TARGET_LITTLE_ENDIAN)
h_u32_to_le(buffer, value);
else
h_u32_to_be(buffer, value);
}{ ... }
void target_buffer_set_u24(struct target *target, uint8_t *buffer, uint32_t value)
{
if (target->endianness == TARGET_LITTLE_ENDIAN)
h_u24_to_le(buffer, value);
else
h_u24_to_be(buffer, value);
}{ ... }
void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
{
if (target->endianness == TARGET_LITTLE_ENDIAN)
h_u16_to_le(buffer, value);
else
h_u16_to_be(buffer, value);
}{ ... }
static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
{
*buffer = value;
}{ ... }
void target_buffer_get_u64_array(struct target *target, const uint8_t *buffer, uint32_t count, uint64_t *dstbuf)
{
uint32_t i;
for (i = 0; i < count; i++)
dstbuf[i] = target_buffer_get_u64(target, &buffer[i * 8]);
}{ ... }
void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf)
{
uint32_t i;
for (i = 0; i < count; i++)
dstbuf[i] = target_buffer_get_u32(target, &buffer[i * 4]);
}{ ... }
void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, uint32_t count, uint16_t *dstbuf)
{
uint32_t i;
for (i = 0; i < count; i++)
dstbuf[i] = target_buffer_get_u16(target, &buffer[i * 2]);
}{ ... }
void target_buffer_set_u64_array(struct target *target, uint8_t *buffer, uint32_t count, const uint64_t *srcbuf)
{
uint32_t i;
for (i = 0; i < count; i++)
target_buffer_set_u64(target, &buffer[i * 8], srcbuf[i]);
}{ ... }
void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, const uint32_t *srcbuf)
{
uint32_t i;
for (i = 0; i < count; i++)
target_buffer_set_u32(target, &buffer[i * 4], srcbuf[i]);
}{ ... }
void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, const uint16_t *srcbuf)
{
uint32_t i;
for (i = 0; i < count; i++)
target_buffer_set_u16(target, &buffer[i * 2], srcbuf[i]);
}{ ... }
struct target *get_target(const char *id)
{
struct target *target;
for (target = all_targets; target; target = target->next) {
if (!target_name(target))
continue;
if (strcmp(id, target_name(target)) == 0)
return target;
}for (target = all_targets; target; target = target->next) { ... }
unsigned int index, counter;
if (parse_uint(id, &index) != ERROR_OK)
return NULL;
for (target = all_targets, counter = index;
target && counter;
target = target->next, --counter)
;
return target;
}{ ... }
struct target *get_current_target(struct command_context *cmd_ctx)
{
struct target *target = get_current_target_or_null(cmd_ctx);
if (!target) {
LOG_ERROR("BUG: current_target out of bounds");
exit(-1);
}if (!target) { ... }
return target;
}{ ... }
struct target *get_current_target_or_null(struct command_context *cmd_ctx)
{
return cmd_ctx->current_target_override
? cmd_ctx->current_target_override
: cmd_ctx->current_target;
}{ ... }
int target_poll(struct target *target)
{
int retval;
if (!target_was_examined(target)) {
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
retval = target->type->poll(target);
if (retval != ERROR_OK)
return retval;
if (target->halt_issued) {
if (target->state == TARGET_HALTED)
target->halt_issued = false;
else {
int64_t t = timeval_ms() - target->halt_issued_time;
if (t > DEFAULT_HALT_TIMEOUT) {
target->halt_issued = false;
LOG_INFO("Halt timed out, wake up GDB.");
target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
}if (t > DEFAULT_HALT_TIMEOUT) { ... }
}else { ... }
}if (target->halt_issued) { ... }
return ERROR_OK;
}{ ... }
int target_halt(struct target *target)
{
int retval;
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
retval = target->type->halt(target);
if (retval != ERROR_OK)
return retval;
target->halt_issued = true;
target->halt_issued_time = timeval_ms();
return ERROR_OK;
}{ ... }
/* ... */
int target_resume(struct target *target, int current, target_addr_t address,
int handle_breakpoints, int debug_execution)
{
int retval;
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
target_call_event_callbacks(target, TARGET_EVENT_RESUME_START);
/* ... */
/* ... */
bool save_poll_mask = jtag_poll_mask();
retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution);
jtag_poll_unmask(save_poll_mask);
if (retval != ERROR_OK)
return retval;
target_call_event_callbacks(target, TARGET_EVENT_RESUME_END);
return retval;
}{ ... }
static int target_process_reset(struct command_invocation *cmd, enum target_reset_mode reset_mode)
{
char buf[100];
int retval;
const struct nvp *n;
n = nvp_value2name(nvp_reset_modes, reset_mode);
if (!n->name) {
LOG_ERROR("invalid reset mode");
return ERROR_FAIL;
}if (!n->name) { ... }
struct target *target;
for (target = all_targets; target; target = target->next)
target_call_reset_callbacks(target, reset_mode);
/* ... */
bool save_poll_mask = jtag_poll_mask();
sprintf(buf, "ocd_process_reset %s", n->name);
retval = Jim_Eval(cmd->ctx->interp, buf);
jtag_poll_unmask(save_poll_mask);
if (retval != JIM_OK) {
Jim_MakeErrorMessage(cmd->ctx->interp);
command_print(cmd, "%s", Jim_GetString(Jim_GetResult(cmd->ctx->interp), NULL));
return ERROR_FAIL;
}if (retval != JIM_OK) { ... }
retval = target_call_timer_callbacks_now();
for (target = all_targets; target; target = target->next) {
target->type->check_reset(target);
target->running_alg = false;
}for (target = all_targets; target; target = target->next) { ... }
return retval;
}{ ... }
static int identity_virt2phys(struct target *target,
target_addr_t virtual, target_addr_t *physical)
{
*physical = virtual;
return ERROR_OK;
}{ ... }
static int no_mmu(struct target *target, int *enabled)
{
*enabled = 0;
return ERROR_OK;
}{ ... }
/* ... */
static inline void target_reset_examined(struct target *target)
{
target->examined = false;
}{ ... }
static int default_examine(struct target *target)
{
target_set_examined(target);
return ERROR_OK;
}{ ... }
static int default_check_reset(struct target *target)
{
return ERROR_OK;
}{ ... }
/* ... */
int target_examine_one(struct target *target)
{
LOG_TARGET_DEBUG(target, "Examination started");
target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_START);
int retval = target->type->examine(target);
if (retval != ERROR_OK) {
LOG_TARGET_ERROR(target, "Examination failed");
LOG_TARGET_DEBUG(target, "examine() returned error code %d", retval);
target_reset_examined(target);
target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_FAIL);
return retval;
}if (retval != ERROR_OK) { ... }
target_set_examined(target);
target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_END);
LOG_TARGET_INFO(target, "Examination succeed");
return ERROR_OK;
}{ ... }
static int jtag_enable_callback(enum jtag_event event, void *priv)
{
struct target *target = priv;
if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
return ERROR_OK;
jtag_unregister_event_callback(jtag_enable_callback, target);
return target_examine_one(target);
}{ ... }
/* ... */
int target_examine(void)
{
int retval = ERROR_OK;
struct target *target;
for (target = all_targets; target; target = target->next) {
if (!target->tap->enabled) {
jtag_register_event_callback(jtag_enable_callback,
target);
continue;
}if (!target->tap->enabled) { ... }
if (target->defer_examine)
continue;
int retval2 = target_examine_one(target);
if (retval2 != ERROR_OK) {
LOG_WARNING("target %s examination failed", target_name(target));
retval = retval2;
}if (retval2 != ERROR_OK) { ... }
}for (target = all_targets; target; target = target->next) { ... }
return retval;
}{ ... }
const char *target_type_name(const struct target *target)
{
return target->type->name;
}{ ... }
static int target_soft_reset_halt(struct target *target)
{
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
if (!target->type->soft_reset_halt) {
LOG_ERROR("Target %s does not support soft_reset_halt",
target_name(target));
return ERROR_FAIL;
}if (!target->type->soft_reset_halt) { ... }
return target->type->soft_reset_halt(target);
}{ ... }
/* ... */
int target_run_algorithm(struct target *target,
int num_mem_params, struct mem_param *mem_params,
int num_reg_params, struct reg_param *reg_param,
target_addr_t entry_point, target_addr_t exit_point,
unsigned int timeout_ms, void *arch_info)
{
int retval = ERROR_FAIL;
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
goto done;
}if (!target_was_examined(target)) { ... }
if (!target->type->run_algorithm) {
LOG_ERROR("Target type '%s' does not support %s",
target_type_name(target), __func__);
goto done;
}if (!target->type->run_algorithm) { ... }
target->running_alg = true;
retval = target->type->run_algorithm(target,
num_mem_params, mem_params,
num_reg_params, reg_param,
entry_point, exit_point, timeout_ms, arch_info);
target->running_alg = false;
done:
return retval;
}{ ... }
/* ... */
int target_start_algorithm(struct target *target,
int num_mem_params, struct mem_param *mem_params,
int num_reg_params, struct reg_param *reg_params,
target_addr_t entry_point, target_addr_t exit_point,
void *arch_info)
{
int retval = ERROR_FAIL;
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
goto done;
}if (!target_was_examined(target)) { ... }
if (!target->type->start_algorithm) {
LOG_ERROR("Target type '%s' does not support %s",
target_type_name(target), __func__);
goto done;
}if (!target->type->start_algorithm) { ... }
if (target->running_alg) {
LOG_ERROR("Target is already running an algorithm");
goto done;
}if (target->running_alg) { ... }
target->running_alg = true;
retval = target->type->start_algorithm(target,
num_mem_params, mem_params,
num_reg_params, reg_params,
entry_point, exit_point, arch_info);
done:
return retval;
}{ ... }
/* ... */
int target_wait_algorithm(struct target *target,
int num_mem_params, struct mem_param *mem_params,
int num_reg_params, struct reg_param *reg_params,
target_addr_t exit_point, unsigned int timeout_ms,
void *arch_info)
{
int retval = ERROR_FAIL;
if (!target->type->wait_algorithm) {
LOG_ERROR("Target type '%s' does not support %s",
target_type_name(target), __func__);
goto done;
}if (!target->type->wait_algorithm) { ... }
if (!target->running_alg) {
LOG_ERROR("Target is not running an algorithm");
goto done;
}if (!target->running_alg) { ... }
retval = target->type->wait_algorithm(target,
num_mem_params, mem_params,
num_reg_params, reg_params,
exit_point, timeout_ms, arch_info);
if (retval != ERROR_TARGET_TIMEOUT)
target->running_alg = false;
done:
return retval;
}{ ... }
/* ... */
int target_run_flash_async_algorithm(struct target *target,
const uint8_t *buffer, uint32_t count, int block_size,
int num_mem_params, struct mem_param *mem_params,
int num_reg_params, struct reg_param *reg_params,
uint32_t buffer_start, uint32_t buffer_size,
uint32_t entry_point, uint32_t exit_point, void *arch_info)
{
int retval;
int timeout = 0;
const uint8_t *buffer_orig = buffer;
uint32_t count_orig = count;
/* ... */
uint32_t wp_addr = buffer_start;
uint32_t rp_addr = buffer_start + 4;
uint32_t fifo_start_addr = buffer_start + 8;
uint32_t fifo_end_addr = buffer_start + buffer_size;
uint32_t wp = fifo_start_addr;
uint32_t rp = fifo_start_addr;
assert(IS_PWR_OF_2(block_size));
retval = target_write_u32(target, wp_addr, wp);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, rp_addr, rp);
if (retval != ERROR_OK)
return retval;
retval = target_start_algorithm(target, num_mem_params, mem_params,
num_reg_params, reg_params,
entry_point,
exit_point,
arch_info);
if (retval != ERROR_OK) {
LOG_ERROR("error starting target flash write algorithm");
return retval;
}if (retval != ERROR_OK) { ... }
if (target->report_flash_progress)
LOG_INFO("flash_write_progress_async_start:0x%x", block_size * count_orig);
while (count > 0) {
retval = target_read_u32(target, rp_addr, &rp);
if (retval != ERROR_OK) {
LOG_ERROR("failed to get read pointer");
break;
}if (retval != ERROR_OK) { ... }
LOG_DEBUG("offs 0x%zx count 0x%" PRIx32 " wp 0x%" PRIx32 " rp 0x%" PRIx32,
(size_t) (buffer - buffer_orig), count, wp, rp);
if (rp == 0) {
LOG_ERROR("flash write algorithm aborted by target");
retval = ERROR_FLASH_OPERATION_FAILED;
break;
}if (rp == 0) { ... }
if (!IS_ALIGNED(rp - fifo_start_addr, block_size) || rp < fifo_start_addr || rp >= fifo_end_addr) {
LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32, rp);
break;
}if (!IS_ALIGNED(rp - fifo_start_addr, block_size) || rp < fifo_start_addr || rp >= fifo_end_addr) { ... }
/* ... */
uint32_t thisrun_bytes;
if (rp > wp)
thisrun_bytes = rp - wp - block_size;
else if (rp > fifo_start_addr)
thisrun_bytes = fifo_end_addr - wp;
else
thisrun_bytes = fifo_end_addr - wp - block_size;
if (thisrun_bytes == 0) {
/* ... */
alive_sleep(2);
/* ... */
if (timeout++ >= 2500) {
LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
return ERROR_FLASH_OPERATION_FAILED;
}if (timeout++ >= 2500) { ... }
continue;
}if (thisrun_bytes == 0) { ... }
timeout = 0;
if (thisrun_bytes > count * block_size)
thisrun_bytes = count * block_size;
if (thisrun_bytes >= 16)
thisrun_bytes -= (rp + thisrun_bytes) & 0x03;
retval = target_write_buffer(target, wp, thisrun_bytes, buffer);
if (retval != ERROR_OK)
break;
buffer += thisrun_bytes;
count -= thisrun_bytes / block_size;
wp += thisrun_bytes;
if (wp >= fifo_end_addr)
wp = fifo_start_addr;
retval = target_write_u32(target, wp_addr, wp);
if (retval != ERROR_OK)
break;
if (target->report_flash_progress)
LOG_INFO("flash_write_progress_async:0x%x|0x%x", buffer - buffer_orig, block_size * count_orig);
keep_alive();
}while (count > 0) { ... }
if (retval != ERROR_OK) {
target_write_u32(target, wp_addr, 0);
}if (retval != ERROR_OK) { ... }
int retval2 = target_wait_algorithm(target, num_mem_params, mem_params,
num_reg_params, reg_params,
exit_point,
10000,
arch_info);
if (retval2 != ERROR_OK) {
LOG_ERROR("error waiting for target flash write algorithm");
retval = retval2;
}if (retval2 != ERROR_OK) { ... }
if (retval == ERROR_OK) {
retval = target_read_u32(target, rp_addr, &rp);
if (retval == ERROR_OK && rp == 0) {
LOG_ERROR("flash write algorithm aborted by target");
retval = ERROR_FLASH_OPERATION_FAILED;
}if (retval == ERROR_OK && rp == 0) { ... }
}if (retval == ERROR_OK) { ... }
return retval;
}{ ... }
int target_run_read_async_algorithm(struct target *target,
uint8_t *buffer, uint32_t count, int block_size,
int num_mem_params, struct mem_param *mem_params,
int num_reg_params, struct reg_param *reg_params,
uint32_t buffer_start, uint32_t buffer_size,
uint32_t entry_point, uint32_t exit_point, void *arch_info)
{
int retval;
int timeout = 0;
const uint8_t *buffer_orig = buffer;
/* ... */
uint32_t wp_addr = buffer_start;
uint32_t rp_addr = buffer_start + 4;
uint32_t fifo_start_addr = buffer_start + 8;
uint32_t fifo_end_addr = buffer_start + buffer_size;
uint32_t wp = fifo_start_addr;
uint32_t rp = fifo_start_addr;
assert(IS_PWR_OF_2(block_size));
retval = target_write_u32(target, wp_addr, wp);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, rp_addr, rp);
if (retval != ERROR_OK)
return retval;
retval = target_start_algorithm(target, num_mem_params, mem_params,
num_reg_params, reg_params,
entry_point,
exit_point,
arch_info);
if (retval != ERROR_OK) {
LOG_ERROR("error starting target flash read algorithm");
return retval;
}if (retval != ERROR_OK) { ... }
while (count > 0) {
retval = target_read_u32(target, wp_addr, &wp);
if (retval != ERROR_OK) {
LOG_ERROR("failed to get write pointer");
break;
}if (retval != ERROR_OK) { ... }
LOG_DEBUG("offs 0x%zx count 0x%" PRIx32 " wp 0x%" PRIx32 " rp 0x%" PRIx32,
(size_t)(buffer - buffer_orig), count, wp, rp);
if (wp == 0) {
LOG_ERROR("flash read algorithm aborted by target");
retval = ERROR_FLASH_OPERATION_FAILED;
break;
}if (wp == 0) { ... }
if (!IS_ALIGNED(wp - fifo_start_addr, block_size) || wp < fifo_start_addr || wp >= fifo_end_addr) {
LOG_ERROR("corrupted fifo write pointer 0x%" PRIx32, wp);
break;
}if (!IS_ALIGNED(wp - fifo_start_addr, block_size) || wp < fifo_start_addr || wp >= fifo_end_addr) { ... }
/* ... */
uint32_t thisrun_bytes;
if (wp >= rp)
thisrun_bytes = wp - rp;
else
thisrun_bytes = fifo_end_addr - rp;
if (thisrun_bytes == 0) {
/* ... */
alive_sleep(2);
/* ... */
if (timeout++ >= 2500) {
LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
return ERROR_FLASH_OPERATION_FAILED;
}if (timeout++ >= 2500) { ... }
continue;
}if (thisrun_bytes == 0) { ... }
timeout = 0;
if (thisrun_bytes > count * block_size)
thisrun_bytes = count * block_size;
if (thisrun_bytes >= 16)
thisrun_bytes -= (rp + thisrun_bytes) & 0x03;
retval = target_read_buffer(target, rp, thisrun_bytes, buffer);
if (retval != ERROR_OK)
break;
buffer += thisrun_bytes;
count -= thisrun_bytes / block_size;
rp += thisrun_bytes;
if (rp >= fifo_end_addr)
rp = fifo_start_addr;
retval = target_write_u32(target, rp_addr, rp);
if (retval != ERROR_OK)
break;
keep_alive();
if (openocd_is_shutdown_pending()) {
retval = ERROR_SERVER_INTERRUPTED;
break;
}if (openocd_is_shutdown_pending()) { ... }
}while (count > 0) { ... }
if (retval != ERROR_OK) {
target_write_u32(target, rp_addr, 0);
}if (retval != ERROR_OK) { ... }
int retval2 = target_wait_algorithm(target, num_mem_params, mem_params,
num_reg_params, reg_params,
exit_point,
10000,
arch_info);
if (retval2 != ERROR_OK) {
LOG_ERROR("error waiting for target flash write algorithm");
retval = retval2;
}if (retval2 != ERROR_OK) { ... }
if (retval == ERROR_OK) {
retval = target_read_u32(target, wp_addr, &wp);
if (retval == ERROR_OK && wp == 0) {
LOG_ERROR("flash read algorithm aborted by target");
retval = ERROR_FLASH_OPERATION_FAILED;
}if (retval == ERROR_OK && wp == 0) { ... }
}if (retval == ERROR_OK) { ... }
return retval;
}{ ... }
int target_read_memory(struct target *target,
target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
{
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
if (!target->type->read_memory) {
LOG_ERROR("Target %s doesn't support read_memory", target_name(target));
return ERROR_FAIL;
}if (!target->type->read_memory) { ... }
return target->type->read_memory(target, address, size, count, buffer);
}{ ... }
int target_read_phys_memory(struct target *target,
target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
{
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
if (!target->type->read_phys_memory) {
LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target));
return ERROR_FAIL;
}if (!target->type->read_phys_memory) { ... }
return target->type->read_phys_memory(target, address, size, count, buffer);
}{ ... }
int target_write_memory(struct target *target,
target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
{
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
if (!target->type->write_memory) {
LOG_ERROR("Target %s doesn't support write_memory", target_name(target));
return ERROR_FAIL;
}if (!target->type->write_memory) { ... }
return target->type->write_memory(target, address, size, count, buffer);
}{ ... }
int target_write_phys_memory(struct target *target,
target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
{
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
if (!target->type->write_phys_memory) {
LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target));
return ERROR_FAIL;
}if (!target->type->write_phys_memory) { ... }
return target->type->write_phys_memory(target, address, size, count, buffer);
}{ ... }
int target_add_breakpoint(struct target *target,
struct breakpoint *breakpoint)
{
if ((target->state != TARGET_HALTED) && (breakpoint->type != BKPT_HARD)) {
LOG_TARGET_ERROR(target, "not halted (add breakpoint)");
return ERROR_TARGET_NOT_HALTED;
}if ((target->state != TARGET_HALTED) && (breakpoint->type != BKPT_HARD)) { ... }
return target->type->add_breakpoint(target, breakpoint);
}{ ... }
int target_add_context_breakpoint(struct target *target,
struct breakpoint *breakpoint)
{
if (target->state != TARGET_HALTED) {
LOG_TARGET_ERROR(target, "not halted (add context breakpoint)");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
return target->type->add_context_breakpoint(target, breakpoint);
}{ ... }
int target_add_hybrid_breakpoint(struct target *target,
struct breakpoint *breakpoint)
{
if (target->state != TARGET_HALTED) {
LOG_TARGET_ERROR(target, "not halted (add hybrid breakpoint)");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
return target->type->add_hybrid_breakpoint(target, breakpoint);
}{ ... }
int target_remove_breakpoint(struct target *target,
struct breakpoint *breakpoint)
{
return target->type->remove_breakpoint(target, breakpoint);
}{ ... }
int target_add_watchpoint(struct target *target,
struct watchpoint *watchpoint)
{
if (target->state != TARGET_HALTED) {
LOG_TARGET_ERROR(target, "not halted (add watchpoint)");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
return target->type->add_watchpoint(target, watchpoint);
}{ ... }
int target_remove_watchpoint(struct target *target,
struct watchpoint *watchpoint)
{
return target->type->remove_watchpoint(target, watchpoint);
}{ ... }
int target_hit_watchpoint(struct target *target,
struct watchpoint **hit_watchpoint)
{
if (target->state != TARGET_HALTED) {
LOG_TARGET_ERROR(target, "not halted (hit watchpoint)");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
if (!target->type->hit_watchpoint) {
/* ... */
return ERROR_FAIL;
}if (!target->type->hit_watchpoint) { ... }
return target->type->hit_watchpoint(target, hit_watchpoint);
}{ ... }
const char *target_get_gdb_arch(const struct target *target)
{
if (!target->type->get_gdb_arch)
return NULL;
return target->type->get_gdb_arch(target);
}{ ... }
int target_get_gdb_reg_list(struct target *target,
struct reg **reg_list[], int *reg_list_size,
enum target_register_class reg_class)
{
int result = ERROR_FAIL;
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
goto done;
}if (!target_was_examined(target)) { ... }
result = target->type->get_gdb_reg_list(target, reg_list,
reg_list_size, reg_class);
done:
if (result != ERROR_OK) {
*reg_list = NULL;
*reg_list_size = 0;
}if (result != ERROR_OK) { ... }
return result;
}{ ... }
int target_get_gdb_reg_list_noread(struct target *target,
struct reg **reg_list[], int *reg_list_size,
enum target_register_class reg_class)
{
if (target->type->get_gdb_reg_list_noread &&
target->type->get_gdb_reg_list_noread(target, reg_list,
reg_list_size, reg_class) == ERROR_OK)
return ERROR_OK;
return target_get_gdb_reg_list(target, reg_list, reg_list_size, reg_class);
}{ ... }
bool target_supports_gdb_connection(const struct target *target)
{
/* ... */
return !!target->type->get_gdb_reg_list && !!target->gdb_max_connections;
}{ ... }
int target_step(struct target *target,
int current, target_addr_t address, int handle_breakpoints)
{
int retval;
if (target->rtos && target->rtos->type->step_hook && target->rtos->type->step_hook(target, current, address, handle_breakpoints) == ERROR_OK)
return ERROR_OK;
target_call_event_callbacks(target, TARGET_EVENT_STEP_START);
retval = target->type->step(target, current, address, handle_breakpoints);
if (retval != ERROR_OK)
return retval;
target_call_event_callbacks(target, TARGET_EVENT_STEP_END);
return retval;
}{ ... }
int target_get_gdb_fileio_info(struct target *target, struct gdb_fileio_info *fileio_info)
{
if (target->state != TARGET_HALTED) {
LOG_TARGET_ERROR(target, "not halted (gdb fileio)");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
return target->type->get_gdb_fileio_info(target, fileio_info);
}{ ... }
int target_gdb_fileio_end(struct target *target, int retcode, int fileio_errno, bool ctrl_c)
{
if (target->state != TARGET_HALTED) {
LOG_TARGET_ERROR(target, "not halted (gdb fileio end)");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
return target->type->gdb_fileio_end(target, retcode, fileio_errno, ctrl_c);
}{ ... }
target_addr_t target_address_max(struct target *target)
{
unsigned bits = target_address_bits(target);
if (sizeof(target_addr_t) * 8 == bits)
return (target_addr_t) -1;
else
return (((target_addr_t) 1) << bits) - 1;
}{ ... }
unsigned target_address_bits(struct target *target)
{
if (target->type->address_bits)
return target->type->address_bits(target);
return 32;
}{ ... }
unsigned int target_data_bits(struct target *target)
{
if (target->type->data_bits)
return target->type->data_bits(target);
return 32;
}{ ... }
static int target_profiling(struct target *target, uint32_t *samples,
uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
{
return target->type->profiling(target, samples, max_num_samples,
num_samples, seconds);
}{ ... }
static int handle_target(void *priv);
static int target_init_one(struct command_context *cmd_ctx,
struct target *target)
{
target_reset_examined(target);
struct target_type *type = target->type;
if (!type->examine)
type->examine = default_examine;
if (!type->check_reset)
type->check_reset = default_check_reset;
assert(type->init_target);
int retval = type->init_target(cmd_ctx, target);
if (retval != ERROR_OK) {
LOG_ERROR("target '%s' init failed", target_name(target));
return retval;
}if (retval != ERROR_OK) { ... }
/* ... */
if (type->mmu) {
if (!type->virt2phys) {
LOG_ERROR("type '%s' is missing virt2phys", target_name(target));
type->virt2phys = identity_virt2phys;
}if (!type->virt2phys) { ... }
}if (type->mmu) { ... } else {
/* ... */
if (type->write_phys_memory || type->read_phys_memory || type->virt2phys)
LOG_WARNING("type '%s' has bad MMU hooks", target_name(target));
type->mmu = no_mmu;
type->write_phys_memory = type->write_memory;
type->read_phys_memory = type->read_memory;
type->virt2phys = identity_virt2phys;
}else { ... }
if (!target->type->read_buffer)
target->type->read_buffer = target_read_buffer_default;
if (!target->type->write_buffer)
target->type->write_buffer = target_write_buffer_default;
if (!target->type->get_gdb_fileio_info)
target->type->get_gdb_fileio_info = target_get_gdb_fileio_info_default;
if (!target->type->gdb_fileio_end)
target->type->gdb_fileio_end = target_gdb_fileio_end_default;
if (!target->type->profiling)
target->type->profiling = target_profiling_default;
return ERROR_OK;
}{ ... }
static int target_init(struct command_context *cmd_ctx)
{
struct target *target;
int retval;
for (target = all_targets; target; target = target->next) {
retval = target_init_one(cmd_ctx, target);
if (retval != ERROR_OK)
return retval;
}for (target = all_targets; target; target = target->next) { ... }
if (!all_targets)
return ERROR_OK;
retval = target_register_user_commands(cmd_ctx);
if (retval != ERROR_OK)
return retval;
retval = target_register_timer_callback(&handle_target,
polling_interval, TARGET_TIMER_TYPE_PERIODIC, cmd_ctx->interp);
if (retval != ERROR_OK)
return retval;
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(handle_target_init_command)
{
int retval;
if (CMD_ARGC != 0)
return ERROR_COMMAND_SYNTAX_ERROR;
static bool target_initialized;
if (target_initialized) {
LOG_INFO("'target init' has already been called");
return ERROR_OK;
}if (target_initialized) { ... }
target_initialized = true;
retval = command_run_line(CMD_CTX, "init_targets");
if (retval != ERROR_OK)
return retval;
retval = command_run_line(CMD_CTX, "init_target_events");
if (retval != ERROR_OK)
return retval;
retval = command_run_line(CMD_CTX, "init_board");
if (retval != ERROR_OK)
return retval;
LOG_DEBUG("Initializing targets...");
return target_init(CMD_CTX);
}{ ... }
int target_register_event_callback(int (*callback)(struct target *target,
enum target_event event, void *priv), void *priv)
{
struct target_event_callback **callbacks_p = &target_event_callbacks;
if (!callback)
return ERROR_COMMAND_SYNTAX_ERROR;
if (*callbacks_p) {
while ((*callbacks_p)->next)
callbacks_p = &((*callbacks_p)->next);
callbacks_p = &((*callbacks_p)->next);
}if (*callbacks_p) { ... }
(*callbacks_p) = malloc(sizeof(struct target_event_callback));
(*callbacks_p)->callback = callback;
(*callbacks_p)->priv = priv;
(*callbacks_p)->next = NULL;
return ERROR_OK;
}{ ... }
int target_register_reset_callback(int (*callback)(struct target *target,
enum target_reset_mode reset_mode, void *priv), void *priv)
{
struct target_reset_callback *entry;
if (!callback)
return ERROR_COMMAND_SYNTAX_ERROR;
entry = malloc(sizeof(struct target_reset_callback));
if (!entry) {
LOG_ERROR("error allocating buffer for reset callback entry");
return ERROR_COMMAND_SYNTAX_ERROR;
}if (!entry) { ... }
entry->callback = callback;
entry->priv = priv;
list_add(&entry->list, &target_reset_callback_list);
return ERROR_OK;
}{ ... }
int target_register_trace_callback(int (*callback)(struct target *target,
size_t len, uint8_t *data, void *priv), void *priv)
{
struct target_trace_callback *entry;
if (!callback)
return ERROR_COMMAND_SYNTAX_ERROR;
entry = malloc(sizeof(struct target_trace_callback));
if (!entry) {
LOG_ERROR("error allocating buffer for trace callback entry");
return ERROR_COMMAND_SYNTAX_ERROR;
}if (!entry) { ... }
entry->callback = callback;
entry->priv = priv;
list_add(&entry->list, &target_trace_callback_list);
return ERROR_OK;
}{ ... }
int target_register_timer_callback(int (*callback)(void *priv),
unsigned int time_ms, enum target_timer_type type, void *priv)
{
struct target_timer_callback **callbacks_p = &target_timer_callbacks;
if (!callback)
return ERROR_COMMAND_SYNTAX_ERROR;
if (*callbacks_p) {
while ((*callbacks_p)->next)
callbacks_p = &((*callbacks_p)->next);
callbacks_p = &((*callbacks_p)->next);
}if (*callbacks_p) { ... }
(*callbacks_p) = malloc(sizeof(struct target_timer_callback));
(*callbacks_p)->callback = callback;
(*callbacks_p)->type = type;
(*callbacks_p)->time_ms = time_ms;
(*callbacks_p)->removed = false;
(*callbacks_p)->when = timeval_ms() + time_ms;
target_timer_next_event_value = MIN(target_timer_next_event_value, (*callbacks_p)->when);
(*callbacks_p)->priv = priv;
(*callbacks_p)->next = NULL;
return ERROR_OK;
}{ ... }
int target_unregister_event_callback(int (*callback)(struct target *target,
enum target_event event, void *priv), void *priv)
{
struct target_event_callback **p = &target_event_callbacks;
struct target_event_callback *c = target_event_callbacks;
if (!callback)
return ERROR_COMMAND_SYNTAX_ERROR;
while (c) {
struct target_event_callback *next = c->next;
if ((c->callback == callback) && (c->priv == priv)) {
*p = next;
free(c);
return ERROR_OK;
}if ((c->callback == callback) && (c->priv == priv)) { ... } else
p = &(c->next);
c = next;
}while (c) { ... }
return ERROR_OK;
}{ ... }
int target_unregister_reset_callback(int (*callback)(struct target *target,
enum target_reset_mode reset_mode, void *priv), void *priv)
{
struct target_reset_callback *entry;
if (!callback)
return ERROR_COMMAND_SYNTAX_ERROR;
list_for_each_entry(entry, &target_reset_callback_list, list) {
if (entry->callback == callback && entry->priv == priv) {
list_del(&entry->list);
free(entry);
break;
}if (entry->callback == callback && entry->priv == priv) { ... }
}list_for_each_entry (entry, &target_reset_callback_list, list) { ... }
return ERROR_OK;
}{ ... }
int target_unregister_trace_callback(int (*callback)(struct target *target,
size_t len, uint8_t *data, void *priv), void *priv)
{
struct target_trace_callback *entry;
if (!callback)
return ERROR_COMMAND_SYNTAX_ERROR;
list_for_each_entry(entry, &target_trace_callback_list, list) {
if (entry->callback == callback && entry->priv == priv) {
list_del(&entry->list);
free(entry);
break;
}if (entry->callback == callback && entry->priv == priv) { ... }
}list_for_each_entry (entry, &target_trace_callback_list, list) { ... }
return ERROR_OK;
}{ ... }
int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
{
if (!callback)
return ERROR_COMMAND_SYNTAX_ERROR;
for (struct target_timer_callback *c = target_timer_callbacks;
c; c = c->next) {
if ((c->callback == callback) && (c->priv == priv)) {
c->removed = true;
return ERROR_OK;
}if ((c->callback == callback) && (c->priv == priv)) { ... }
}for (struct target_timer_callback *c = target_timer_callbacks; c; c = c->next) { ... }
return ERROR_FAIL;
}{ ... }
int target_call_event_callbacks(struct target *target, enum target_event event)
{
struct target_event_callback *callback = target_event_callbacks;
struct target_event_callback *next_callback;
if (event == TARGET_EVENT_HALTED) {
target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
}if (event == TARGET_EVENT_HALTED) { ... }
LOG_DEBUG("target event %i (%s) for core %s", event,
target_event_name(event),
target_name(target));
target_handle_event(target, event);
while (callback) {
next_callback = callback->next;
callback->callback(target, event, callback->priv);
callback = next_callback;
}while (callback) { ... }
return ERROR_OK;
}{ ... }
int target_call_reset_callbacks(struct target *target, enum target_reset_mode reset_mode)
{
struct target_reset_callback *callback;
LOG_DEBUG("target reset %i (%s)", reset_mode,
nvp_value2name(nvp_reset_modes, reset_mode)->name);
list_for_each_entry(callback, &target_reset_callback_list, list)
callback->callback(target, reset_mode, callback->priv);
return ERROR_OK;
}{ ... }
int target_call_trace_callbacks(struct target *target, size_t len, uint8_t *data)
{
struct target_trace_callback *callback;
list_for_each_entry(callback, &target_trace_callback_list, list)
callback->callback(target, len, data, callback->priv);
return ERROR_OK;
}{ ... }
static int target_timer_callback_periodic_restart(
struct target_timer_callback *cb, int64_t *now)
{
cb->when = *now + cb->time_ms;
return ERROR_OK;
}{ ... }
static int target_call_timer_callback(struct target_timer_callback *cb,
int64_t *now)
{
cb->callback(cb->priv);
if (cb->type == TARGET_TIMER_TYPE_PERIODIC)
return target_timer_callback_periodic_restart(cb, now);
return target_unregister_timer_callback(cb->callback, cb->priv);
}{ ... }
static int target_call_timer_callbacks_check_time(int checktime)
{
static bool callback_processing;
if (callback_processing)
return ERROR_OK;
callback_processing = true;
keep_alive();
int64_t now = timeval_ms();
/* ... */
target_timer_next_event_value = now + 1000;
/* ... */
struct target_timer_callback **callback = &target_timer_callbacks;
while (callback && *callback) {
if ((*callback)->removed) {
struct target_timer_callback *p = *callback;
*callback = (*callback)->next;
free(p);
continue;
}if ((*callback)->removed) { ... }
bool call_it = (*callback)->callback &&
((!checktime && (*callback)->type == TARGET_TIMER_TYPE_PERIODIC) ||
now >= (*callback)->when);
if (call_it)
target_call_timer_callback(*callback, &now);
if (!(*callback)->removed && (*callback)->when < target_timer_next_event_value)
target_timer_next_event_value = (*callback)->when;
callback = &(*callback)->next;
}while (callback && *callback) { ... }
callback_processing = false;
return ERROR_OK;
}{ ... }
int target_call_timer_callbacks(void)
{
return target_call_timer_callbacks_check_time(1);
}{ ... }
int target_call_timer_callbacks_now(void)
{
return target_call_timer_callbacks_check_time(0);
}{ ... }
int64_t target_timer_next_event(void)
{
return target_timer_next_event_value;
}{ ... }
static void print_wa_layout(struct target *target)
{
struct working_area *c = target->working_areas;
while (c) {
LOG_DEBUG("%c%c " TARGET_ADDR_FMT "-" TARGET_ADDR_FMT " (%" PRIu32 " bytes)",
c->backup ? 'b' : ' ', c->free ? ' ' : '*',
c->address, c->address + c->size - 1, c->size);
c = c->next;
}while (c) { ... }
}{ ... }
static void target_split_working_area(struct working_area *area, uint32_t size)
{
assert(area->free);
assert(size <= area->size);
if (size < area->size) {
struct working_area *new_wa = malloc(sizeof(*new_wa));
if (!new_wa)
return;
new_wa->next = area->next;
new_wa->size = area->size - size;
new_wa->address = area->address + size;
new_wa->backup = NULL;
new_wa->user = NULL;
new_wa->free = true;
area->next = new_wa;
area->size = size;
/* ... */
free(area->backup);
area->backup = NULL;
}if (size < area->size) { ... }
}{ ... }
static void target_merge_working_areas(struct target *target)
{
struct working_area *c = target->working_areas;
while (c && c->next) {
assert(c->next->address == c->address + c->size);
if (c->free && c->next->free) {
c->size += c->next->size;
struct working_area *to_be_freed = c->next;
c->next = c->next->next;
free(to_be_freed->backup);
free(to_be_freed);
/* ... */
free(c->backup);
c->backup = NULL;
}if (c->free && c->next->free) { ... } else {
c = c->next;
}else { ... }
}while (c && c->next) { ... }
}{ ... }
int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
{
if (!target->working_areas) {
int retval;
int enabled;
retval = target->type->mmu(target, &enabled);
if (retval != ERROR_OK)
return retval;
if (!enabled) {
if (target->working_area_phys_spec) {
LOG_DEBUG("MMU disabled, using physical "
"address for working memory " TARGET_ADDR_FMT,
target->working_area_phys);
target->working_area = target->working_area_phys;
}if (target->working_area_phys_spec) { ... } else {
LOG_ERROR("No working memory available. "
"Specify -work-area-phys to target.");
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
}else { ... }
}if (!enabled) { ... } else {
if (target->working_area_virt_spec) {
LOG_DEBUG("MMU enabled, using virtual "
"address for working memory " TARGET_ADDR_FMT,
target->working_area_virt);
target->working_area = target->working_area_virt;
}if (target->working_area_virt_spec) { ... } else {
LOG_ERROR("No working memory available. "
"Specify -work-area-virt to target.");
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
}else { ... }
}else { ... }
struct working_area *new_wa = malloc(sizeof(*new_wa));
if (new_wa) {
new_wa->next = NULL;
new_wa->size = ALIGN_DOWN(target->working_area_size, 4);
new_wa->address = target->working_area;
new_wa->backup = NULL;
new_wa->user = NULL;
new_wa->free = true;
}if (new_wa) { ... }
target->working_areas = new_wa;
}if (!target->working_areas) { ... }
size = ALIGN_UP(size, 4);
struct working_area *c = target->working_areas;
while (c) {
if (c->free && c->size >= size)
break;
c = c->next;
}while (c) { ... }
if (!c)
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
target_split_working_area(c, size);
LOG_DEBUG("allocated new working area of %" PRIu32 " bytes at address " TARGET_ADDR_FMT,
size, c->address);
if (target->backup_working_area) {
if (!c->backup) {
c->backup = malloc(c->size);
if (!c->backup)
return ERROR_FAIL;
}if (!c->backup) { ... }
int retval = target_read_memory(target, c->address, 4, c->size / 4, c->backup);
if (retval != ERROR_OK)
return retval;
}if (target->backup_working_area) { ... }
c->free = false;
*area = c;
c->user = area;
print_wa_layout(target);
return ERROR_OK;
}{ ... }
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
{
int retval;
retval = target_alloc_working_area_try(target, size, area);
if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
LOG_WARNING("not enough working area available(requested %"PRIu32")", size);
return retval;
}{ ... }
static int target_restore_working_area(struct target *target, struct working_area *area)
{
int retval = ERROR_OK;
if (target->backup_working_area && area->backup) {
retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup);
if (retval != ERROR_OK)
LOG_ERROR("failed to restore %" PRIu32 " bytes of working area at address " TARGET_ADDR_FMT,
area->size, area->address);
}if (target->backup_working_area && area->backup) { ... }
return retval;
}{ ... }
static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
{
if (!area || area->free)
return ERROR_OK;
int retval = ERROR_OK;
if (restore) {
retval = target_restore_working_area(target, area);
if (retval != ERROR_OK)
return retval;
}if (restore) { ... }
area->free = true;
LOG_DEBUG("freed %" PRIu32 " bytes of working area at address " TARGET_ADDR_FMT,
area->size, area->address);
/* ... */
*area->user = NULL;
area->user = NULL;
target_merge_working_areas(target);
print_wa_layout(target);
return retval;
}{ ... }
int target_free_working_area(struct target *target, struct working_area *area)
{
return target_free_working_area_restore(target, area, 1);
}{ ... }
/* ... */
static void target_free_all_working_areas_restore(struct target *target, int restore)
{
struct working_area *c = target->working_areas;
LOG_DEBUG("freeing all working areas");
while (c) {
if (!c->free) {
if (restore)
target_restore_working_area(target, c);
c->free = true;
*c->user = NULL;
c->user = NULL;
}if (!c->free) { ... }
c = c->next;
}while (c) { ... }
target_merge_working_areas(target);
print_wa_layout(target);
}{ ... }
void target_free_all_working_areas(struct target *target)
{
target_free_all_working_areas_restore(target, 1);
if (target->working_areas) {
free(target->working_areas->backup);
free(target->working_areas);
target->working_areas = NULL;
}if (target->working_areas) { ... }
}{ ... }
uint32_t target_get_working_area_avail(struct target *target)
{
struct working_area *c = target->working_areas;
uint32_t max_size = 0;
if (!c)
return ALIGN_DOWN(target->working_area_size, 4);
while (c) {
if (c->free && max_size < c->size)
max_size = c->size;
c = c->next;
}while (c) { ... }
return max_size;
}{ ... }
static void target_destroy(struct target *target)
{
breakpoint_remove_all(target);
watchpoint_remove_all(target);
if (target->type->deinit_target)
target->type->deinit_target(target);
if (target->semihosting)
free(target->semihosting->basedir);
free(target->semihosting);
jtag_unregister_event_callback(jtag_enable_callback, target);
struct target_event_action *teap = target->event_action;
while (teap) {
struct target_event_action *next = teap->next;
Jim_DecrRefCount(teap->interp, teap->body);
free(teap);
teap = next;
}while (teap) { ... }
target_free_all_working_areas(target);
if (target->smp) {
struct target_list *head, *tmp;
list_for_each_entry_safe(head, tmp, target->smp_targets, lh) {
list_del(&head->lh);
head->target->smp = 0;
free(head);
}list_for_each_entry_safe (head, tmp, target->smp_targets, lh) { ... }
if (target->smp_targets != &empty_smp_targets)
free(target->smp_targets);
target->smp = 0;
}if (target->smp) { ... }
rtos_destroy(target);
free(target->gdb_port_override);
free(target->type);
free(target->trace_info);
free(target->fileio_info);
free(target->cmd_name);
free(target);
}{ ... }
void target_quit(void)
{
struct target_event_callback *pe = target_event_callbacks;
while (pe) {
struct target_event_callback *t = pe->next;
free(pe);
pe = t;
}while (pe) { ... }
target_event_callbacks = NULL;
struct target_timer_callback *pt = target_timer_callbacks;
while (pt) {
struct target_timer_callback *t = pt->next;
free(pt);
pt = t;
}while (pt) { ... }
target_timer_callbacks = NULL;
for (struct target *target = all_targets; target;) {
struct target *tmp;
tmp = target->next;
target_destroy(target);
target = tmp;
}for (struct target *target = all_targets; target;) { ... }
all_targets = NULL;
}{ ... }
int target_arch_state(struct target *target)
{
int retval;
if (!target) {
LOG_WARNING("No target has been configured");
return ERROR_OK;
}if (!target) { ... }
if (target->state != TARGET_HALTED)
return ERROR_OK;
retval = target->type->arch_state(target);
return retval;
}{ ... }
static int target_get_gdb_fileio_info_default(struct target *target,
struct gdb_fileio_info *fileio_info)
{
/* ... */
return ERROR_FAIL;
}{ ... }
static int target_gdb_fileio_end_default(struct target *target,
int retcode, int fileio_errno, bool ctrl_c)
{
return ERROR_OK;
}{ ... }
int target_profiling_default(struct target *target, uint32_t *samples,
uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
{
struct timeval timeout, now;
gettimeofday(&timeout, NULL);
timeval_add_time(&timeout, seconds, 0);
LOG_INFO("Starting profiling. Halting and resuming the"
" target as often as we can...");
uint32_t sample_count = 0;
struct reg *reg = register_get_by_name(target->reg_cache, "pc", true);
int retval = ERROR_OK;
for (;;) {
target_poll(target);
if (target->state == TARGET_HALTED) {
uint32_t t = buf_get_u32(reg->value, 0, 32);
samples[sample_count++] = t;
retval = target_resume(target, 1, 0, 0, 0);
target_poll(target);
alive_sleep(10);
}if (target->state == TARGET_HALTED) { ... } else if (target->state == TARGET_RUNNING) {
retval = target_halt(target);
}else if (target->state == TARGET_RUNNING) { ... } else {
LOG_INFO("Target not halted or running");
retval = ERROR_OK;
break;
}else { ... }
if (retval != ERROR_OK)
break;
gettimeofday(&now, NULL);
if ((sample_count >= max_num_samples) || timeval_compare(&now, &timeout) >= 0) {
LOG_INFO("Profiling completed. %" PRIu32 " samples.", sample_count);
break;
}if ((sample_count >= max_num_samples) || timeval_compare(&now, &timeout) >= 0) { ... }
}for (;;) { ... }
*num_samples = sample_count;
return retval;
}{ ... }
/* ... */
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
{
LOG_DEBUG("writing buffer of %" PRIu32 " byte at " TARGET_ADDR_FMT,
size, address);
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
if (size == 0)
return ERROR_OK;
if ((address + size - 1) < address) {
LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT ", 0x%08" PRIx32 ")",
address,
size);
return ERROR_FAIL;
}if ((address + size - 1) < address) { ... }
return target->type->write_buffer(target, address, size, buffer);
}{ ... }
static int target_write_buffer_default(struct target *target,
target_addr_t address, uint32_t count, const uint8_t *buffer)
{
uint32_t size;
unsigned int data_bytes = target_data_bits(target) / 8;
/* ... */
for (size = 1;
size < data_bytes && count >= size * 2 + (address & size);
size *= 2) {
if (address & size) {
int retval = target_write_memory(target, address, size, 1, buffer);
if (retval != ERROR_OK)
return retval;
address += size;
count -= size;
buffer += size;
}if (address & size) { ... }
}for (size = 1; size < data_bytes && count >= size * 2 + (address & size); size *= 2) { ... }
for (; size > 0; size /= 2) {
uint32_t aligned = count - count % size;
if (aligned > 0) {
int retval = target_write_memory(target, address, size, aligned / size, buffer);
if (retval != ERROR_OK)
return retval;
address += aligned;
count -= aligned;
buffer += aligned;
}if (aligned > 0) { ... }
}for (; size > 0; size /= 2) { ... }
return ERROR_OK;
}{ ... }
/* ... */
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
{
LOG_DEBUG("reading buffer of %" PRIu32 " byte at " TARGET_ADDR_FMT,
size, address);
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
if (size == 0)
return ERROR_OK;
if ((address + size - 1) < address) {
LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT ", 0x%08" PRIx32 ")",
address,
size);
return ERROR_FAIL;
}if ((address + size - 1) < address) { ... }
return target->type->read_buffer(target, address, size, buffer);
}{ ... }
static int target_read_buffer_default(struct target *target, target_addr_t address, uint32_t count, uint8_t *buffer)
{
uint32_t size;
unsigned int data_bytes = target_data_bits(target) / 8;
/* ... */
for (size = 1;
size < data_bytes && count >= size * 2 + (address & size);
size *= 2) {
if (address & size) {
int retval = target_read_memory(target, address, size, 1, buffer);
if (retval != ERROR_OK)
return retval;
address += size;
count -= size;
buffer += size;
}if (address & size) { ... }
}for (size = 1; size < data_bytes && count >= size * 2 + (address & size); size *= 2) { ... }
for (; size > 0; size /= 2) {
uint32_t aligned = count - count % size;
if (aligned > 0) {
int retval = target_read_memory(target, address, size, aligned / size, buffer);
if (retval != ERROR_OK)
return retval;
address += aligned;
count -= aligned;
buffer += aligned;
}if (aligned > 0) { ... }
}for (; size > 0; size /= 2) { ... }
return ERROR_OK;
}{ ... }
int target_checksum_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t *crc)
{
uint8_t *buffer;
int retval;
uint32_t i;
uint32_t checksum = 0;
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
if (!target->type->checksum_memory) {
LOG_ERROR("Target %s doesn't support checksum_memory", target_name(target));
return ERROR_FAIL;
}if (!target->type->checksum_memory) { ... }
retval = target->type->checksum_memory(target, address, size, &checksum);
if (retval != ERROR_OK) {
buffer = malloc(size);
if (!buffer) {
LOG_ERROR("error allocating buffer for section (%" PRIu32 " bytes)", size);
return ERROR_COMMAND_SYNTAX_ERROR;
}if (!buffer) { ... }
retval = target_read_buffer(target, address, size, buffer);
if (retval != ERROR_OK) {
free(buffer);
return retval;
}if (retval != ERROR_OK) { ... }
for (i = 0; i < (size/sizeof(uint32_t)); i++) {
uint32_t target_data;
target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
}for (i = 0; i < (size/sizeof(uint32_t)); i++) { ... }
retval = image_calculate_checksum(buffer, size, &checksum);
free(buffer);
}if (retval != ERROR_OK) { ... }
*crc = checksum;
return retval;
}{ ... }
int target_blank_check_memory(struct target *target,
struct target_memory_check_block *blocks, int num_blocks,
uint8_t erased_value)
{
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
if (!target->type->blank_check_memory)
return ERROR_NOT_IMPLEMENTED;
return target->type->blank_check_memory(target, blocks, num_blocks, erased_value);
}{ ... }
int target_read_u64(struct target *target, target_addr_t address, uint64_t *value)
{
uint8_t value_buf[8];
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
int retval = target_read_memory(target, address, 8, 1, value_buf);
if (retval == ERROR_OK) {
*value = target_buffer_get_u64(target, value_buf);
LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64 "",
address,
*value);
}if (retval == ERROR_OK) { ... } else {
*value = 0x0;
LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
address);
}else { ... }
return retval;
}{ ... }
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
{
uint8_t value_buf[4];
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
int retval = target_read_memory(target, address, 4, 1, value_buf);
if (retval == ERROR_OK) {
*value = target_buffer_get_u32(target, value_buf);
LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32 "",
address,
*value);
}if (retval == ERROR_OK) { ... } else {
*value = 0x0;
LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
address);
}else { ... }
return retval;
}{ ... }
int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
{
uint8_t value_buf[2];
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
int retval = target_read_memory(target, address, 2, 1, value_buf);
if (retval == ERROR_OK) {
*value = target_buffer_get_u16(target, value_buf);
LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%4.4" PRIx16,
address,
*value);
}if (retval == ERROR_OK) { ... } else {
*value = 0x0;
LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
address);
}else { ... }
return retval;
}{ ... }
int target_read_u8(struct target *target, target_addr_t address, uint8_t *value)
{
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
int retval = target_read_memory(target, address, 1, 1, value);
if (retval == ERROR_OK) {
LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
address,
*value);
}if (retval == ERROR_OK) { ... } else {
*value = 0x0;
LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
address);
}else { ... }
return retval;
}{ ... }
int target_write_u64(struct target *target, target_addr_t address, uint64_t value)
{
int retval;
uint8_t value_buf[8];
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64 "",
address,
value);
target_buffer_set_u64(target, value_buf, value);
retval = target_write_memory(target, address, 8, 1, value_buf);
if (retval != ERROR_OK)
LOG_DEBUG("failed: %i", retval);
return retval;
}{ ... }
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
{
int retval;
uint8_t value_buf[4];
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32 "",
address,
value);
target_buffer_set_u32(target, value_buf, value);
retval = target_write_memory(target, address, 4, 1, value_buf);
if (retval != ERROR_OK)
LOG_DEBUG("failed: %i", retval);
return retval;
}{ ... }
int target_write_u16(struct target *target, target_addr_t address, uint16_t value)
{
int retval;
uint8_t value_buf[2];
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
address,
value);
target_buffer_set_u16(target, value_buf, value);
retval = target_write_memory(target, address, 2, 1, value_buf);
if (retval != ERROR_OK)
LOG_DEBUG("failed: %i", retval);
return retval;
}{ ... }
int target_write_u8(struct target *target, target_addr_t address, uint8_t value)
{
int retval;
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
address, value);
retval = target_write_memory(target, address, 1, 1, &value);
if (retval != ERROR_OK)
LOG_DEBUG("failed: %i", retval);
return retval;
}{ ... }
int target_write_phys_u64(struct target *target, target_addr_t address, uint64_t value)
{
int retval;
uint8_t value_buf[8];
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64 "",
address,
value);
target_buffer_set_u64(target, value_buf, value);
retval = target_write_phys_memory(target, address, 8, 1, value_buf);
if (retval != ERROR_OK)
LOG_DEBUG("failed: %i", retval);
return retval;
}{ ... }
int target_write_phys_u32(struct target *target, target_addr_t address, uint32_t value)
{
int retval;
uint8_t value_buf[4];
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32 "",
address,
value);
target_buffer_set_u32(target, value_buf, value);
retval = target_write_phys_memory(target, address, 4, 1, value_buf);
if (retval != ERROR_OK)
LOG_DEBUG("failed: %i", retval);
return retval;
}{ ... }
int target_write_phys_u16(struct target *target, target_addr_t address, uint16_t value)
{
int retval;
uint8_t value_buf[2];
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
address,
value);
target_buffer_set_u16(target, value_buf, value);
retval = target_write_phys_memory(target, address, 2, 1, value_buf);
if (retval != ERROR_OK)
LOG_DEBUG("failed: %i", retval);
return retval;
}{ ... }
int target_write_phys_u8(struct target *target, target_addr_t address, uint8_t value)
{
int retval;
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}if (!target_was_examined(target)) { ... }
LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
address, value);
retval = target_write_phys_memory(target, address, 1, 1, &value);
if (retval != ERROR_OK)
LOG_DEBUG("failed: %i", retval);
return retval;
}{ ... }
static int find_target(struct command_invocation *cmd, const char *name)
{
struct target *target = get_target(name);
if (!target) {
command_print(cmd, "Target: %s is unknown, try one of:\n", name);
return ERROR_FAIL;
}if (!target) { ... }
if (!target->tap->enabled) {
command_print(cmd, "Target: TAP %s is disabled, "
"can't be the current target\n",
target->tap->dotted_name);
return ERROR_FAIL;
}if (!target->tap->enabled) { ... }
cmd->ctx->current_target = target;
if (cmd->ctx->current_target_override)
cmd->ctx->current_target_override = target;
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(handle_targets_command)
{
int retval = ERROR_OK;
if (CMD_ARGC == 1) {
retval = find_target(CMD, CMD_ARGV[0]);
if (retval == ERROR_OK) {
return retval;
}if (retval == ERROR_OK) { ... }
}if (CMD_ARGC == 1) { ... }
unsigned int index = 0;
command_print(CMD, " TargetName Type Endian TapName State ");
command_print(CMD, "-- ------------------ ---------- ------ ------------------ ------------");
for (struct target *target = all_targets; target; target = target->next, ++index) {
const char *state;
char marker = ' ';
if (target->tap->enabled)
state = target_state_name(target);
else
state = "tap-disabled";
if (CMD_CTX->current_target == target)
marker = '*';
command_print(CMD,
"%2d%c %-18s %-10s %-6s %-18s %s",
index,
marker,
target_name(target),
target_type_name(target),
jim_nvp_value2name_simple(nvp_target_endian,
target->endianness)->name,
target->tap->dotted_name,
state);
}for (struct target *target = all_targets; target; target = target->next, ++index) { ... }
return retval;
}{ ... }
static int power_dropout;
static int srst_asserted;
static int run_power_restore;
static int run_power_dropout;
static int run_srst_asserted;
static int run_srst_deasserted;
static int sense_handler(void)
{
static int prev_srst_asserted;
static int prev_power_dropout;
int retval = jtag_power_dropout(&power_dropout);
if (retval != ERROR_OK)
return retval;
int power_restored;
power_restored = prev_power_dropout && !power_dropout;
if (power_restored)
run_power_restore = 1;
int64_t current = timeval_ms();
static int64_t last_power;
bool wait_more = last_power + 2000 > current;
if (power_dropout && !wait_more) {
run_power_dropout = 1;
last_power = current;
}if (power_dropout && !wait_more) { ... }
retval = jtag_srst_asserted(&srst_asserted);
if (retval != ERROR_OK)
return retval;
int srst_deasserted;
srst_deasserted = prev_srst_asserted && !srst_asserted;
static int64_t last_srst;
wait_more = last_srst + 2000 > current;
if (srst_deasserted && !wait_more) {
run_srst_deasserted = 1;
last_srst = current;
}if (srst_deasserted && !wait_more) { ... }
if (!prev_srst_asserted && srst_asserted)
run_srst_asserted = 1;
prev_srst_asserted = srst_asserted;
prev_power_dropout = power_dropout;
if (srst_deasserted || power_restored) {
/* ... */
}if (srst_deasserted || power_restored) { ... }
return ERROR_OK;
}{ ... }
static int handle_target(void *priv)
{
Jim_Interp *interp = (Jim_Interp *)priv;
int retval = ERROR_OK;
if (!is_jtag_poll_safe()) {
return ERROR_OK;
}if (!is_jtag_poll_safe()) { ... }
static int recursive;
if (!recursive) {
recursive = 1;
sense_handler();
/* ... */
int did_something = 0;
if (run_srst_asserted) {
LOG_INFO("srst asserted detected, running srst_asserted proc.");
Jim_Eval(interp, "srst_asserted");
did_something = 1;
}if (run_srst_asserted) { ... }
if (run_srst_deasserted) {
Jim_Eval(interp, "srst_deasserted");
did_something = 1;
}if (run_srst_deasserted) { ... }
if (run_power_dropout) {
LOG_INFO("Power dropout detected, running power_dropout proc.");
Jim_Eval(interp, "power_dropout");
did_something = 1;
}if (run_power_dropout) { ... }
if (run_power_restore) {
Jim_Eval(interp, "power_restore");
did_something = 1;
}if (run_power_restore) { ... }
if (did_something) {
sense_handler();
}if (did_something) { ... }
run_srst_asserted = 0;
run_srst_deasserted = 0;
run_power_restore = 0;
run_power_dropout = 0;
recursive = 0;
}if (!recursive) { ... }
/* ... */
for (struct target *target = all_targets;
is_jtag_poll_safe() && target;
target = target->next) {
if (!target_was_examined(target))
continue;
if (!target->tap->enabled)
continue;
if (target->backoff.times > target->backoff.count) {
target->backoff.count++;
continue;
}if (target->backoff.times > target->backoff.count) { ... }
target->backoff.count = 0;
if (!power_dropout && !srst_asserted) {
retval = target_poll(target);
if (retval != ERROR_OK) {
if (target->backoff.times * polling_interval < 5000) {
target->backoff.times *= 2;
target->backoff.times++;
}if (target->backoff.times * polling_interval < 5000) { ... }
/* ... */
target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
}if (retval != ERROR_OK) { ... }
if (target->backoff.times > 0) {
LOG_TARGET_ERROR(target, "Polling failed, trying to reexamine");
target_reset_examined(target);
retval = target_examine_one(target);
/* ... */
if (retval != ERROR_OK) {
target_set_examined(target);
LOG_TARGET_ERROR(target, "Examination failed, GDB will be halted. Polling again in %dms",
target->backoff.times * polling_interval);
return retval;
}if (retval != ERROR_OK) { ... }
}if (target->backoff.times > 0) { ... }
target->backoff.times = 0;
}if (!power_dropout && !srst_asserted) { ... }
}for (struct target *target = all_targets; is_jtag_poll_safe() && target; target = target->next) { ... }
return retval;
}{ ... }
COMMAND_HANDLER(handle_reg_command)
{
LOG_DEBUG("-");
struct target *target = get_current_target(CMD_CTX);
if (!target_was_examined(target)) {
LOG_ERROR("Target not examined yet");
return ERROR_TARGET_NOT_EXAMINED;
}if (!target_was_examined(target)) { ... }
struct reg *reg = NULL;
if (CMD_ARGC == 0) {
struct reg_cache *cache = target->reg_cache;
unsigned int count = 0;
while (cache) {
unsigned i;
command_print(CMD, "===== %s", cache->name);
for (i = 0, reg = cache->reg_list;
i < cache->num_regs;
i++, reg++, count++) {
if (reg->exist == false || reg->hidden)
continue;
if (reg->valid) {
char *value = buf_to_hex_str(reg->value,
reg->size);
command_print(CMD,
"(%i) %s (/%" PRIu32 "): 0x%s%s",
count, reg->name,
reg->size, value,
reg->dirty
? " (dirty)"
: "");
free(value);
}if (reg->valid) { ... } else {
command_print(CMD, "(%i) %s (/%" PRIu32 ")",
count, reg->name,
reg->size);
}else { ... }
}for (i = 0, reg = cache->reg_list; i < cache->num_regs; i++, reg++, count++) { ... }
cache = cache->next;
}while (cache) { ... }
return ERROR_OK;
}if (CMD_ARGC == 0) { ... }
if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9')) {
unsigned num;
COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
struct reg_cache *cache = target->reg_cache;
unsigned int count = 0;
while (cache) {
unsigned i;
for (i = 0; i < cache->num_regs; i++) {
if (count++ == num) {
reg = &cache->reg_list[i];
break;
}if (count++ == num) { ... }
}for (i = 0; i < cache->num_regs; i++) { ... }
if (reg)
break;
cache = cache->next;
}while (cache) { ... }
if (!reg) {
command_print(CMD, "%i is out of bounds, the current target "
"has only %i registers (0 - %i)", num, count, count - 1);
return ERROR_FAIL;
}if (!reg) { ... }
}if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9')) { ... } else {
reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], true);
if (!reg)
goto not_found;
}else { ... }
assert(reg);
if (!reg->exist)
goto not_found;
if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0')
&& (CMD_ARGV[1][0] <= '9')))) {
if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
reg->valid = false;
if (!reg->valid) {
int retval = reg->type->get(reg);
if (retval != ERROR_OK) {
LOG_ERROR("Could not read register '%s'", reg->name);
return retval;
}if (retval != ERROR_OK) { ... }
}if (!reg->valid) { ... }
char *value = buf_to_hex_str(reg->value, reg->size);
command_print(CMD, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
free(value);
return ERROR_OK;
}if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0') && (CMD_ARGV[1][0] <= '9')))) { ... }
if (CMD_ARGC == 2) {
uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
if (!buf) {
LOG_ERROR("Failed to allocate memory");
return ERROR_FAIL;
}if (!buf) { ... }
int retval = CALL_COMMAND_HANDLER(command_parse_str_to_buf, CMD_ARGV[1], buf, reg->size);
if (retval != ERROR_OK) {
free(buf);
return retval;
}if (retval != ERROR_OK) { ... }
retval = reg->type->set(reg, buf);
if (retval != ERROR_OK) {
LOG_ERROR("Could not write to register '%s'", reg->name);
}if (retval != ERROR_OK) { ... } else {
char *value = buf_to_hex_str(reg->value, reg->size);
command_print(CMD, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
free(value);
}else { ... }
free(buf);
return retval;
}if (CMD_ARGC == 2) { ... }
return ERROR_COMMAND_SYNTAX_ERROR;
not_found:
command_print(CMD, "register %s not found in current target", CMD_ARGV[0]);
return ERROR_FAIL;
}{ ... }
COMMAND_HANDLER(handle_poll_command)
{
int retval = ERROR_OK;
struct target *target = get_current_target(CMD_CTX);
if (CMD_ARGC == 0) {
command_print(CMD, "background polling: %s",
jtag_poll_get_enabled() ? "on" : "off");
command_print(CMD, "TAP: %s (%s)",
target->tap->dotted_name,
target->tap->enabled ? "enabled" : "disabled");
if (!target->tap->enabled)
return ERROR_OK;
retval = target_poll(target);
if (retval != ERROR_OK)
return retval;
retval = target_arch_state(target);
if (retval != ERROR_OK)
return retval;
}if (CMD_ARGC == 0) { ... } else if (CMD_ARGC == 1) {
bool enable;
COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
jtag_poll_set_enabled(enable);
}else if (CMD_ARGC == 1) { ... } else
return ERROR_COMMAND_SYNTAX_ERROR;
return retval;
}{ ... }
COMMAND_HANDLER(handle_wait_halt_command)
{
if (CMD_ARGC > 1)
return ERROR_COMMAND_SYNTAX_ERROR;
unsigned ms = DEFAULT_HALT_TIMEOUT;
if (1 == CMD_ARGC) {
int retval = parse_uint(CMD_ARGV[0], &ms);
if (retval != ERROR_OK)
return ERROR_COMMAND_SYNTAX_ERROR;
}if (1 == CMD_ARGC) { ... }
struct target *target = get_current_target(CMD_CTX);
return target_wait_state(target, TARGET_HALTED, ms);
}{ ... }
/* ... */
int target_wait_state(struct target *target, enum target_state state, unsigned int ms)
{
int retval;
int64_t then = 0, cur;
bool once = true;
for (;;) {
retval = target_poll(target);
if (retval != ERROR_OK)
return retval;
if (target->state == state)
break;
cur = timeval_ms();
if (once) {
once = false;
then = timeval_ms();
LOG_DEBUG("waiting for target %s...",
nvp_value2name(nvp_target_state, state)->name);
}if (once) { ... }
if (cur - then > 500) {
keep_alive();
if (openocd_is_shutdown_pending())
return ERROR_SERVER_INTERRUPTED;
}if (cur - then > 500) { ... }
if (ms >= 0 && ((cur-then) > ms)) {
LOG_ERROR("timed out while waiting for target %s",
nvp_value2name(nvp_target_state, state)->name);
return ERROR_FAIL;
}if (ms >= 0 && ((cur-then) > ms)) { ... }
}for (;;) { ... }
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(handle_halt_command)
{
LOG_DEBUG("-");
struct target *target = get_current_target(CMD_CTX);
target->verbose_halt_msg = true;
int retval = target_halt(target);
if (retval != ERROR_OK)
return retval;
if (CMD_ARGC == 1) {
unsigned wait_local;
retval = parse_uint(CMD_ARGV[0], &wait_local);
if (retval != ERROR_OK)
return ERROR_COMMAND_SYNTAX_ERROR;
if (!wait_local)
return ERROR_OK;
}if (CMD_ARGC == 1) { ... }
return CALL_COMMAND_HANDLER(handle_wait_halt_command);
}{ ... }
COMMAND_HANDLER(handle_soft_reset_halt_command)
{
struct target *target = get_current_target(CMD_CTX);
LOG_TARGET_INFO(target, "requesting target halt and executing a soft reset");
target_soft_reset_halt(target);
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(handle_reset_command)
{
if (CMD_ARGC > 1)
return ERROR_COMMAND_SYNTAX_ERROR;
enum target_reset_mode reset_mode = RESET_RUN;
if (CMD_ARGC == 1) {
const struct nvp *n;
n = nvp_name2value(nvp_reset_modes, CMD_ARGV[0]);
if ((!n->name) || (n->value == RESET_UNKNOWN))
return ERROR_COMMAND_SYNTAX_ERROR;
reset_mode = n->value;
}if (CMD_ARGC == 1) { ... }
return target_process_reset(CMD, reset_mode);
}{ ... }
COMMAND_HANDLER(handle_resume_command)
{
int current = 1;
if (CMD_ARGC > 1)
return ERROR_COMMAND_SYNTAX_ERROR;
struct target *target = get_current_target(CMD_CTX);
/* ... */
target_addr_t addr = 0;
if (CMD_ARGC == 1) {
COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
current = 0;
}if (CMD_ARGC == 1) { ... }
return target_resume(target, current, addr, 1, 0);
}{ ... }
COMMAND_HANDLER(handle_step_command)
{
if (CMD_ARGC > 1)
return ERROR_COMMAND_SYNTAX_ERROR;
LOG_DEBUG("-");
/* ... */
target_addr_t addr = 0;
int current_pc = 1;
if (CMD_ARGC == 1) {
COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
current_pc = 0;
}if (CMD_ARGC == 1) { ... }
struct target *target = get_current_target(CMD_CTX);
return target_step(target, current_pc, addr, 1);
}{ ... }
void target_handle_md_output(struct command_invocation *cmd,
struct target *target, target_addr_t address, unsigned size,
unsigned count, const uint8_t *buffer)
{
const unsigned line_bytecnt = 32;
unsigned line_modulo = line_bytecnt / size;
char output[line_bytecnt * 4 + 1];
unsigned output_len = 0;
const char *value_fmt;
switch (size) {
case 8:
value_fmt = "%16.16"PRIx64" ";
break;case 8:
case 4:
value_fmt = "%8.8"PRIx64" ";
break;case 4:
case 2:
value_fmt = "%4.4"PRIx64" ";
break;case 2:
case 1:
value_fmt = "%2.2"PRIx64" ";
break;case 1:
default:
LOG_ERROR("invalid memory read size: %u", size);
return;default
}switch (size) { ... }
for (unsigned i = 0; i < count; i++) {
if (i % line_modulo == 0) {
output_len += snprintf(output + output_len,
sizeof(output) - output_len,
TARGET_ADDR_FMT ": ",
(address + (i * size)));
}if (i % line_modulo == 0) { ... }
uint64_t value = 0;
const uint8_t *value_ptr = buffer + i * size;
switch (size) {
case 8:
value = target_buffer_get_u64(target, value_ptr);
break;case 8:
case 4:
value = target_buffer_get_u32(target, value_ptr);
break;case 4:
case 2:
value = target_buffer_get_u16(target, value_ptr);
break;case 2:
case 1:
value = *value_ptr;case 1:
}switch (size) { ... }
output_len += snprintf(output + output_len,
sizeof(output) - output_len,
value_fmt, value);
if ((i % line_modulo == line_modulo - 1) || (i == count - 1)) {
command_print(cmd, "%s", output);
output_len = 0;
}if ((i % line_modulo == line_modulo - 1) || (i == count - 1)) { ... }
}for (unsigned i = 0; i < count; i++) { ... }
}{ ... }
COMMAND_HANDLER(handle_md_command)
{
if (CMD_ARGC < 1)
return ERROR_COMMAND_SYNTAX_ERROR;
unsigned size = 0;
switch (CMD_NAME[2]) {
case 'd':
size = 8;
break;case 'd':
case 'w':
size = 4;
break;case 'w':
case 'h':
size = 2;
break;case 'h':
case 'b':
size = 1;
break;case 'b':
default:
return ERROR_COMMAND_SYNTAX_ERROR;default
}switch (CMD_NAME[2]) { ... }
bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
int (*fn)(struct target *target,
target_addr_t address, uint32_t size_value, uint32_t count, uint8_t *buffer);
if (physical) {
CMD_ARGC--;
CMD_ARGV++;
fn = target_read_phys_memory;
}if (physical) { ... } else
fn = target_read_memory;
if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
return ERROR_COMMAND_SYNTAX_ERROR;
target_addr_t address;
COMMAND_PARSE_ADDRESS(CMD_ARGV[0], address);
unsigned count = 1;
if (CMD_ARGC == 2)
COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
uint8_t *buffer = calloc(count, size);
if (!buffer) {
LOG_ERROR("Failed to allocate md read buffer");
return ERROR_FAIL;
}if (!buffer) { ... }
struct target *target = get_current_target(CMD_CTX);
int retval = fn(target, address, size, count, buffer);
if (retval == ERROR_OK)
target_handle_md_output(CMD, target, address, size, count, buffer);
else
{
command_print(cmd, "0x%08x: ERROR %d", address, retval);
}else { ... }
free(buffer);
return retval;
}{ ... }
typedef int (*target_write_fn)(struct target *target,
target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer);
static int target_fill_mem(struct target *target,
target_addr_t address,
target_write_fn fn,
unsigned data_size,
uint64_t b,
unsigned c)
{
/* ... */
const unsigned chunk_size = 16384;
uint8_t *target_buf = malloc(chunk_size * data_size);
if (!target_buf) {
LOG_ERROR("Out of memory");
return ERROR_FAIL;
}if (!target_buf) { ... }
for (unsigned i = 0; i < chunk_size; i++) {
switch (data_size) {
case 8:
target_buffer_set_u64(target, target_buf + i * data_size, b);
break;case 8:
case 4:
target_buffer_set_u32(target, target_buf + i * data_size, b);
break;case 4:
case 2:
target_buffer_set_u16(target, target_buf + i * data_size, b);
break;case 2:
case 1:
target_buffer_set_u8(target, target_buf + i * data_size, b);
break;case 1:
default:
exit(-1);default
}switch (data_size) { ... }
}for (unsigned i = 0; i < chunk_size; i++) { ... }
int retval = ERROR_OK;
for (unsigned x = 0; x < c; x += chunk_size) {
unsigned current;
current = c - x;
if (current > chunk_size)
current = chunk_size;
retval = fn(target, address + x * data_size, data_size, current, target_buf);
if (retval != ERROR_OK)
break;
keep_alive();
if (openocd_is_shutdown_pending()) {
retval = ERROR_SERVER_INTERRUPTED;
break;
}if (openocd_is_shutdown_pending()) { ... }
}for (unsigned x = 0; x < c; x += chunk_size) { ... }
free(target_buf);
return retval;
}{ ... }
COMMAND_HANDLER(handle_mw_command)
{
if (CMD_ARGC < 2)
return ERROR_COMMAND_SYNTAX_ERROR;
bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
target_write_fn fn;
if (physical) {
CMD_ARGC--;
CMD_ARGV++;
fn = target_write_phys_memory;
}if (physical) { ... } else
fn = target_write_memory;
if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
return ERROR_COMMAND_SYNTAX_ERROR;
target_addr_t address;
COMMAND_PARSE_ADDRESS(CMD_ARGV[0], address);
uint64_t value;
COMMAND_PARSE_NUMBER(u64, CMD_ARGV[1], value);
unsigned count = 1;
if (CMD_ARGC == 3)
COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
struct target *target = get_current_target(CMD_CTX);
unsigned wordsize;
switch (CMD_NAME[2]) {
case 'd':
wordsize = 8;
break;case 'd':
case 'w':
wordsize = 4;
break;case 'w':
case 'h':
wordsize = 2;
break;case 'h':
case 'b':
wordsize = 1;
break;case 'b':
default:
return ERROR_COMMAND_SYNTAX_ERROR;default
}switch (CMD_NAME[2]) { ... }
return target_fill_mem(target, address, fn, wordsize, value, count);
}{ ... }
COMMAND_HANDLER(handle_mbatch_command)
{
if (CMD_ARGC < 2)
return ERROR_COMMAND_SYNTAX_ERROR;
/* ... */
struct target *target = get_current_target(CMD_CTX);
command_print_sameline(cmd, "mbatch(%s):", CMD_ARGV[0]);
for (int i = 1; i < CMD_ARGC; i++)
{
const char *pCmd = CMD_ARGV[i];
const char *p = strchr(pCmd, ':');
int status = ERROR_OK;
uint8_t *data = NULL;
int printData = 0;
uint64_t args[3] = { 0, };
if (!p)
status = -ERROR_BAD_ARGUMENTS;
for (int j = 0; j < sizeof(args) / sizeof(args[0]); j++)
{
if (p)
{
p++;
char *end = NULL;
args[j] = strtoull(p, &end, 0);
if (end == p || !end)
status = -ERROR_BAD_ARGUMENTS;
else if (*end != ':' && *end != 0)
status = -ERROR_BAD_ARGUMENTS;
p = end;
}if (p) { ... }
}for (int j = 0; j < sizeof(args) / sizeof(args[0]); j++) { ... }
int bufferSize = (int)(args[1] * args[2]);
if (status == ERROR_OK)
{
data = (uint8_t *)malloc(bufferSize);
if (pCmd[0] == 'w')
{
if (!p)
status = -ERROR_BAD_ARGUMENTS;
else
{
int done = unhexify(data, p + 1, args[1] * args[2]);
if (done != (args[1] * args[2]))
status = -ERROR_INSUFFICIENT_BUFFER;
else
status = target_write_memory(target, args[0], args[1], args[2], data);
}else { ... }
}if (pCmd[0] == 'w') { ... }
else if (pCmd[0] == 'r')
{
status = target_read_memory(target, args[0], args[1], args[2], data);
printData = true;
}else if (pCmd[0] == 'r') { ... }
else
status = -ERROR_BAD_ARGUMENTS;
}if (status == ERROR_OK) { ... }
command_print_sameline(cmd, " %d", status);
if (status >= 0 && printData && data)
{
char *formattedData = malloc(bufferSize * 2 + 2);
hexify(formattedData, data, bufferSize, bufferSize * 2 + 2);
command_print_sameline(cmd, ":%s", formattedData);
free(formattedData);
}if (status >= 0 && printData && data) { ... }
if (data)
free(data);
}for (int i = 1; i < CMD_ARGC; i++) { ... }
command_print(cmd, "");
return ERROR_OK;
}{ ... }
static COMMAND_HELPER(parse_load_image_command, struct image *image,
target_addr_t *min_address, target_addr_t *max_address)
{
if (CMD_ARGC < 1 || CMD_ARGC > 5)
return ERROR_COMMAND_SYNTAX_ERROR;
/* ... */
if (CMD_ARGC >= 2) {
target_addr_t addr;
COMMAND_PARSE_ADDRESS(CMD_ARGV[1], addr);
image->base_address = addr;
image->base_address_set = true;
}if (CMD_ARGC >= 2) { ... } else
image->base_address_set = false;
image->start_address_set = false;
if (CMD_ARGC >= 4)
COMMAND_PARSE_ADDRESS(CMD_ARGV[3], *min_address);
if (CMD_ARGC == 5) {
COMMAND_PARSE_ADDRESS(CMD_ARGV[4], *max_address);
*max_address += *min_address;
}if (CMD_ARGC == 5) { ... }
if (*min_address > *max_address)
return ERROR_COMMAND_SYNTAX_ERROR;
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(handle_load_image_command)
{
uint8_t *buffer;
size_t buf_cnt;
uint32_t image_size;
target_addr_t min_address = 0;
target_addr_t max_address = -1;
struct image image;
int retval = CALL_COMMAND_HANDLER(parse_load_image_command,
&image, &min_address, &max_address);
if (retval != ERROR_OK)
return retval;
struct target *target = get_current_target(CMD_CTX);
struct duration bench;
duration_start(&bench);
if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
return ERROR_FAIL;
image_size = 0x0;
retval = ERROR_OK;
for (unsigned int i = 0; i < image.num_sections; i++) {
buffer = malloc(image.sections[i].size);
if (!buffer) {
command_print(CMD,
"error allocating buffer for section (%d bytes)",
(int)(image.sections[i].size));
retval = ERROR_FAIL;
break;
}if (!buffer) { ... }
retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
if (retval != ERROR_OK) {
free(buffer);
break;
}if (retval != ERROR_OK) { ... }
uint32_t offset = 0;
uint32_t length = buf_cnt;
if ((image.sections[i].base_address + buf_cnt >= min_address) &&
(image.sections[i].base_address < max_address)) {
if (image.sections[i].base_address < min_address) {
offset += min_address-image.sections[i].base_address;
length -= offset;
}if (image.sections[i].base_address < min_address) { ... }
if (image.sections[i].base_address + buf_cnt > max_address)
length -= (image.sections[i].base_address + buf_cnt)-max_address;
retval = target_write_buffer(target,
image.sections[i].base_address + offset, length, buffer + offset);
if (retval != ERROR_OK) {
free(buffer);
break;
}if (retval != ERROR_OK) { ... }
image_size += length;
command_print(CMD, "%u bytes written at address " TARGET_ADDR_FMT "",
(unsigned int)length,
image.sections[i].base_address + offset);
}if ((image.sections[i].base_address + buf_cnt >= min_address) && (image.sections[i].base_address < max_address)) { ... }
free(buffer);
}for (unsigned int i = 0; i < image.num_sections; i++) { ... }
if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
command_print(CMD, "downloaded %" PRIu32 " bytes "
"in %fs (%0.3f KiB/s)", image_size,
duration_elapsed(&bench), duration_kbps(&bench, image_size));
}if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) { ... }
image_close(&image);
return retval;
}{ ... }
COMMAND_HANDLER(handle_dump_image_command)
{
struct fileio *fileio;
uint8_t *buffer;
int retval, retvaltemp;
target_addr_t address, size;
struct duration bench;
struct target *target = get_current_target(CMD_CTX);
if (CMD_ARGC != 3)
return ERROR_COMMAND_SYNTAX_ERROR;
COMMAND_PARSE_ADDRESS(CMD_ARGV[1], address);
COMMAND_PARSE_ADDRESS(CMD_ARGV[2], size);
uint32_t buf_size = (size > 4096) ? 4096 : size;
buffer = malloc(buf_size);
if (!buffer)
return ERROR_FAIL;
retval = fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY);
if (retval != ERROR_OK) {
free(buffer);
return retval;
}if (retval != ERROR_OK) { ... }
duration_start(&bench);
while (size > 0) {
size_t size_written;
uint32_t this_run_size = (size > buf_size) ? buf_size : size;
retval = target_read_buffer(target, address, this_run_size, buffer);
if (retval != ERROR_OK)
break;
retval = fileio_write(fileio, this_run_size, buffer, &size_written);
if (retval != ERROR_OK)
break;
size -= this_run_size;
address += this_run_size;
}while (size > 0) { ... }
free(buffer);
if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
size_t filesize;
retval = fileio_size(fileio, &filesize);
if (retval != ERROR_OK)
return retval;
command_print(CMD,
"dumped %zu bytes in %fs (%0.3f KiB/s)", filesize,
duration_elapsed(&bench), duration_kbps(&bench, filesize));
}if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) { ... }
retvaltemp = fileio_close(fileio);
if (retvaltemp != ERROR_OK)
return retvaltemp;
return retval;
}{ ... }
enum verify_mode {
IMAGE_TEST = 0,
IMAGE_VERIFY = 1,
IMAGE_CHECKSUM_ONLY = 2
...};
static COMMAND_HELPER(handle_verify_image_command_internal, enum verify_mode verify)
{
uint8_t *buffer;
size_t buf_cnt;
uint32_t image_size;
int retval;
uint32_t checksum = 0;
uint32_t mem_checksum = 0;
struct image image;
struct target *target = get_current_target(CMD_CTX);
if (CMD_ARGC < 1)
return ERROR_COMMAND_SYNTAX_ERROR;
if (!target) {
LOG_ERROR("no target selected");
return ERROR_FAIL;
}if (!target) { ... }
struct duration bench;
duration_start(&bench);
if (CMD_ARGC >= 2) {
target_addr_t addr;
COMMAND_PARSE_ADDRESS(CMD_ARGV[1], addr);
image.base_address = addr;
image.base_address_set = true;
}if (CMD_ARGC >= 2) { ... } else {
image.base_address_set = false;
image.base_address = 0x0;
}else { ... }
image.start_address_set = false;
retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
if (retval != ERROR_OK)
return retval;
image_size = 0x0;
int diffs = 0;
retval = ERROR_OK;
for (unsigned int i = 0; i < image.num_sections; i++) {
buffer = malloc(image.sections[i].size);
if (!buffer) {
command_print(CMD,
"error allocating buffer for section (%" PRIu32 " bytes)",
image.sections[i].size);
break;
}if (!buffer) { ... }
retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
if (retval != ERROR_OK) {
free(buffer);
break;
}if (retval != ERROR_OK) { ... }
if (verify >= IMAGE_VERIFY) {
retval = image_calculate_checksum(buffer, buf_cnt, &checksum);
if (retval != ERROR_OK) {
free(buffer);
break;
}if (retval != ERROR_OK) { ... }
retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
if (retval != ERROR_OK) {
free(buffer);
break;
}if (retval != ERROR_OK) { ... }
if ((checksum != mem_checksum) && (verify == IMAGE_CHECKSUM_ONLY)) {
LOG_ERROR("checksum mismatch");
free(buffer);
retval = ERROR_FAIL;
goto done;
}if ((checksum != mem_checksum) && (verify == IMAGE_CHECKSUM_ONLY)) { ... }
if (checksum != mem_checksum) {
uint8_t *data;
if (diffs == 0)
LOG_ERROR("checksum mismatch - attempting binary compare");
data = malloc(buf_cnt);
retval = target_read_buffer(target, image.sections[i].base_address, buf_cnt, data);
if (retval == ERROR_OK) {
uint32_t t;
for (t = 0; t < buf_cnt; t++) {
if (data[t] != buffer[t]) {
command_print(CMD,
"diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
diffs,
(unsigned)(t + image.sections[i].base_address),
data[t],
buffer[t]);
if (diffs++ >= 127) {
command_print(CMD, "More than 128 errors, the rest are not printed.");
free(data);
free(buffer);
goto done;
}if (diffs++ >= 127) { ... }
}if (data[t] != buffer[t]) { ... }
keep_alive();
if (openocd_is_shutdown_pending()) {
retval = ERROR_SERVER_INTERRUPTED;
free(data);
free(buffer);
goto done;
}if (openocd_is_shutdown_pending()) { ... }
}for (t = 0; t < buf_cnt; t++) { ... }
}if (retval == ERROR_OK) { ... }
free(data);
}if (checksum != mem_checksum) { ... }
}if (verify >= IMAGE_VERIFY) { ... } else {
command_print(CMD, "address " TARGET_ADDR_FMT " length 0x%08zx",
image.sections[i].base_address,
buf_cnt);
}else { ... }
free(buffer);
image_size += buf_cnt;
}for (unsigned int i = 0; i < image.num_sections; i++) { ... }
if (diffs > 0)
command_print(CMD, "No more differences found.");
done:
if (diffs > 0)
retval = ERROR_FAIL;
if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
command_print(CMD, "verified %" PRIu32 " bytes "
"in %fs (%0.3f KiB/s)", image_size,
duration_elapsed(&bench), duration_kbps(&bench, image_size));
}if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) { ... }
image_close(&image);
return retval;
}{ ... }
COMMAND_HANDLER(handle_verify_image_checksum_command)
{
return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_CHECKSUM_ONLY);
}{ ... }
COMMAND_HANDLER(handle_verify_image_command)
{
return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_VERIFY);
}{ ... }
COMMAND_HANDLER(handle_test_image_command)
{
return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_TEST);
}{ ... }
static int handle_bp_command_list(struct command_invocation *cmd)
{
struct target *target = get_current_target(cmd->ctx);
struct breakpoint *breakpoint = target->breakpoints;
while (breakpoint) {
if (breakpoint->type == BKPT_SOFT) {
char *buf = buf_to_hex_str(breakpoint->orig_instr,
breakpoint->length * 8);
command_print(cmd, "Software breakpoint(IVA): addr=" TARGET_ADDR_FMT ", len=0x%x, orig_instr=0x%s",
breakpoint->address,
breakpoint->length,
buf);
free(buf);
}if (breakpoint->type == BKPT_SOFT) { ... } else {
if ((breakpoint->address == 0) && (breakpoint->asid != 0))
command_print(cmd, "Context breakpoint: asid=0x%8.8" PRIx32 ", len=0x%x, num=%u",
breakpoint->asid,
breakpoint->length, breakpoint->number);
else if ((breakpoint->address != 0) && (breakpoint->asid != 0)) {
command_print(cmd, "Hybrid breakpoint(IVA): addr=" TARGET_ADDR_FMT ", len=0x%x, num=%u",
breakpoint->address,
breakpoint->length, breakpoint->number);
command_print(cmd, "\t|--->linked with ContextID: 0x%8.8" PRIx32,
breakpoint->asid);
}else if ((breakpoint->address != 0) && (breakpoint->asid != 0)) { ... } else
command_print(cmd, "Hardware breakpoint(IVA): addr=" TARGET_ADDR_FMT ", len=0x%x, num=%u",
breakpoint->address,
breakpoint->length, breakpoint->number);
}else { ... }
breakpoint = breakpoint->next;
}while (breakpoint) { ... }
return ERROR_OK;
}{ ... }
static int handle_bp_command_set(struct command_invocation *cmd,
target_addr_t addr, uint32_t asid, unsigned int length, int hw)
{
struct target *target = get_current_target(cmd->ctx);
int retval;
if (asid == 0) {
retval = breakpoint_add(target, addr, length, hw);
if (retval == ERROR_OK)
command_print(cmd, "breakpoint set at " TARGET_ADDR_FMT "", addr);
}if (asid == 0) { ... } else if (addr == 0) {
if (!target->type->add_context_breakpoint) {
LOG_TARGET_ERROR(target, "Context breakpoint not available");
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
}if (!target->type->add_context_breakpoint) { ... }
retval = context_breakpoint_add(target, asid, length, hw);
if (retval == ERROR_OK)
command_print(cmd, "Context breakpoint set at 0x%8.8" PRIx32 "", asid);
}else if (addr == 0) { ... } else {
if (!target->type->add_hybrid_breakpoint) {
LOG_TARGET_ERROR(target, "Hybrid breakpoint not available");
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
}if (!target->type->add_hybrid_breakpoint) { ... }
retval = hybrid_breakpoint_add(target, addr, asid, length, hw);
if (retval == ERROR_OK)
command_print(cmd, "Hybrid breakpoint set at 0x%8.8" PRIx32 "", asid);
}else { ... }
return retval;
}{ ... }
COMMAND_HANDLER(handle_bp_command)
{
target_addr_t addr;
uint32_t asid;
uint32_t length;
int hw = BKPT_SOFT;
switch (CMD_ARGC) {
case 0:
return handle_bp_command_list(CMD);
case 0:
case 2:
asid = 0;
COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
return handle_bp_command_set(CMD, addr, asid, length, hw);
case 2:
case 3:
if (strcmp(CMD_ARGV[2], "hw") == 0) {
hw = BKPT_HARD;
COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
asid = 0;
return handle_bp_command_set(CMD, addr, asid, length, hw);
}if (strcmp(CMD_ARGV[2], "hw") == 0) { ... } else if (strcmp(CMD_ARGV[2], "hw_ctx") == 0) {
hw = BKPT_HARD;
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], asid);
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
addr = 0;
return handle_bp_command_set(CMD, addr, asid, length, hw);
}else if (strcmp(CMD_ARGV[2], "hw_ctx") == 0) { ... }
case 3:
case 4:
hw = BKPT_HARD;
COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], asid);
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], length);
return handle_bp_command_set(CMD, addr, asid, length, hw);
case 4:
default:
return ERROR_COMMAND_SYNTAX_ERROR;default
}switch (CMD_ARGC) { ... }
}{ ... }
COMMAND_HANDLER(handle_rbp_command)
{
int retval;
if (CMD_ARGC != 1)
return ERROR_COMMAND_SYNTAX_ERROR;
struct target *target = get_current_target(CMD_CTX);
if (!strcmp(CMD_ARGV[0], "all")) {
retval = breakpoint_remove_all(target);
if (retval != ERROR_OK) {
command_print(CMD, "Error encountered during removal of all breakpoints.");
command_print(CMD, "Some breakpoints may have remained set.");
}if (retval != ERROR_OK) { ... }
}if (!strcmp(CMD_ARGV[0], "all")) { ... } else {
target_addr_t addr;
COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
retval = breakpoint_remove(target, addr);
if (retval != ERROR_OK)
command_print(CMD, "Error during removal of breakpoint at address " TARGET_ADDR_FMT, addr);
}else { ... }
return retval;
}{ ... }
COMMAND_HANDLER(handle_wp_command)
{
struct target *target = get_current_target(CMD_CTX);
if (CMD_ARGC == 0) {
struct watchpoint *watchpoint = target->watchpoints;
while (watchpoint) {
char wp_type = (watchpoint->rw == WPT_READ ? 'r' : (watchpoint->rw == WPT_WRITE ? 'w' : 'a'));
command_print(CMD, "address: " TARGET_ADDR_FMT
", len: 0x%8.8x"
", r/w/a: %c, value: 0x%8.8" PRIx64
", mask: 0x%8.8" PRIx64,
watchpoint->address,
watchpoint->length,
wp_type,
watchpoint->value,
watchpoint->mask);
watchpoint = watchpoint->next;
}while (watchpoint) { ... }
return ERROR_OK;
}if (CMD_ARGC == 0) { ... }
enum watchpoint_rw type = WPT_ACCESS;
target_addr_t addr = 0;
uint32_t length = 0;
uint64_t data_value = 0x0;
uint64_t data_mask = WATCHPOINT_IGNORE_DATA_VALUE_MASK;
bool mask_specified = false;
switch (CMD_ARGC) {
case 5:
COMMAND_PARSE_NUMBER(u64, CMD_ARGV[4], data_mask);
mask_specified = true;
case 5:
case 4:
COMMAND_PARSE_NUMBER(u64, CMD_ARGV[3], data_value);
if (!mask_specified)
data_mask = 0;
case 4:
case 3:
switch (CMD_ARGV[2][0]) {
case 'r':
type = WPT_READ;
break;case 'r':
case 'w':
type = WPT_WRITE;
break;case 'w':
case 'a':
type = WPT_ACCESS;
break;case 'a':
default:
LOG_TARGET_ERROR(target, "invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
return ERROR_COMMAND_SYNTAX_ERROR;default
}switch (CMD_ARGV[2][0]) { ... }
case 3:
case 2:
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
break;
case 2:
default:
return ERROR_COMMAND_SYNTAX_ERROR;default
}switch (CMD_ARGC) { ... }
int retval = watchpoint_add(target, addr, length, type,
data_value, data_mask);
if (retval != ERROR_OK)
LOG_TARGET_ERROR(target, "Failure setting watchpoints");
return retval;
}{ ... }
COMMAND_HANDLER(handle_rwp_command)
{
int retval;
if (CMD_ARGC != 1)
return ERROR_COMMAND_SYNTAX_ERROR;
struct target *target = get_current_target(CMD_CTX);
if (!strcmp(CMD_ARGV[0], "all")) {
retval = watchpoint_remove_all(target);
if (retval != ERROR_OK) {
command_print(CMD, "Error encountered during removal of all watchpoints.");
command_print(CMD, "Some watchpoints may have remained set.");
}if (retval != ERROR_OK) { ... }
}if (!strcmp(CMD_ARGV[0], "all")) { ... } else {
target_addr_t addr;
COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
retval = watchpoint_remove(target, addr);
if (retval != ERROR_OK)
command_print(CMD, "Error during removal of watchpoint at address " TARGET_ADDR_FMT, addr);
}else { ... }
return retval;
}{ ... }
/* ... */
COMMAND_HANDLER(handle_virt2phys_command)
{
if (CMD_ARGC != 1)
return ERROR_COMMAND_SYNTAX_ERROR;
target_addr_t va;
COMMAND_PARSE_ADDRESS(CMD_ARGV[0], va);
target_addr_t pa;
struct target *target = get_current_target(CMD_CTX);
int retval = target->type->virt2phys(target, va, &pa);
if (retval == ERROR_OK)
command_print(CMD, "Physical address " TARGET_ADDR_FMT "", pa);
return retval;
}{ ... }
static void write_data(FILE *f, const void *data, size_t len)
{
size_t written = fwrite(data, 1, len, f);
if (written != len)
LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
}{ ... }
static void write_long(FILE *f, int l, struct target *target)
{
uint8_t val[4];
target_buffer_set_u32(target, val, l);
write_data(f, val, 4);
}{ ... }
static void write_string(FILE *f, char *s)
{
write_data(f, s, strlen(s));
}{ ... }
typedef unsigned char UNIT[2];
static void write_gmon(uint32_t *samples, uint32_t sample_num, const char *filename, bool with_range,
uint32_t start_address, uint32_t end_address, struct target *target, uint32_t duration_ms)
{
uint32_t i;
FILE *f = fopen(filename, "wb");
if (!f)
return;
write_string(f, "gmon");
write_long(f, 0x00000001, target);
write_long(f, 0, target);
write_long(f, 0, target);
write_long(f, 0, target);
uint8_t zero = 0;
write_data(f, &zero, 1);
uint32_t min;
uint32_t max;
if (with_range) {
min = start_address;
max = end_address;
}if (with_range) { ... } else {
min = samples[0];
max = samples[0];
for (i = 0; i < sample_num; i++) {
if (min > samples[i])
min = samples[i];
if (max < samples[i])
max = samples[i];
}for (i = 0; i < sample_num; i++) { ... }
/* ... */
if (max < UINT32_MAX)
max++;
while ((max - min) < 2) {
if (max < UINT32_MAX)
max++;
else
min--;
}while ((max - min) < 2) { ... }
}else { ... }
uint32_t address_space = max - min;
/* ... */
static const uint32_t max_buckets = 128 * 1024;
uint32_t num_buckets = address_space / sizeof(UNIT);
if (num_buckets > max_buckets)
num_buckets = max_buckets;
int *buckets = malloc(sizeof(int) * num_buckets);
if (!buckets) {
fclose(f);
return;
}if (!buckets) { ... }
memset(buckets, 0, sizeof(int) * num_buckets);
for (i = 0; i < sample_num; i++) {
uint32_t address = samples[i];
if ((address < min) || (max <= address))
continue;
long long a = address - min;
long long b = num_buckets;
long long c = address_space;
int index_t = (a * b) / c;
buckets[index_t]++;
}for (i = 0; i < sample_num; i++) { ... }
write_long(f, min, target);
write_long(f, max, target);
write_long(f, num_buckets, target);
float sample_rate = sample_num / (duration_ms / 1000.0);
write_long(f, sample_rate, target);
write_string(f, "seconds");
for (i = 0; i < (15-strlen("seconds")); i++)
write_data(f, &zero, 1);
write_string(f, "s");
char *data = malloc(2 * num_buckets);
if (data) {
for (i = 0; i < num_buckets; i++) {
int val;
val = buckets[i];
if (val > 65535)
val = 65535;
data[i * 2] = val&0xff;
data[i * 2 + 1] = (val >> 8) & 0xff;
}for (i = 0; i < num_buckets; i++) { ... }
free(buckets);
write_data(f, data, num_buckets * 2);
free(data);
}if (data) { ... } else
free(buckets);
fclose(f);
}{ ... }
/* ... */
COMMAND_HANDLER(handle_profile_command)
{
struct target *target = get_current_target(CMD_CTX);
if ((CMD_ARGC != 2) && (CMD_ARGC != 4))
return ERROR_COMMAND_SYNTAX_ERROR;
const uint32_t MAX_PROFILE_SAMPLE_NUM = 1000000;
uint32_t offset;
uint32_t num_of_samples;
int retval = ERROR_OK;
bool halted_before_profiling = target->state == TARGET_HALTED;
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], offset);
uint32_t start_address = 0;
uint32_t end_address = 0;
bool with_range = false;
if (CMD_ARGC == 4) {
with_range = true;
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], start_address);
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], end_address);
if (start_address > end_address || (end_address - start_address) < 2) {
command_print(CMD, "Error: end - start < 2");
return ERROR_COMMAND_ARGUMENT_INVALID;
}if (start_address > end_address || (end_address - start_address) < 2) { ... }
}if (CMD_ARGC == 4) { ... }
uint32_t *samples = malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM);
if (!samples) {
LOG_ERROR("No memory to store samples.");
return ERROR_FAIL;
}if (!samples) { ... }
uint64_t timestart_ms = timeval_ms();
/* ... */
retval = target_profiling(target, samples, MAX_PROFILE_SAMPLE_NUM,
&num_of_samples, offset);
if (retval != ERROR_OK) {
free(samples);
return retval;
}if (retval != ERROR_OK) { ... }
uint32_t duration_ms = timeval_ms() - timestart_ms;
assert(num_of_samples <= MAX_PROFILE_SAMPLE_NUM);
retval = target_poll(target);
if (retval != ERROR_OK) {
free(samples);
return retval;
}if (retval != ERROR_OK) { ... }
if (target->state == TARGET_RUNNING && halted_before_profiling) {
/* ... */
retval = target_halt(target);
if (retval != ERROR_OK) {
free(samples);
return retval;
}if (retval != ERROR_OK) { ... }
}if (target->state == TARGET_RUNNING && halted_before_profiling) { ... } else if (target->state == TARGET_HALTED && !halted_before_profiling) {
/* ... */
retval = target_resume(target, 1, 0, 0, 0);
if (retval != ERROR_OK) {
free(samples);
return retval;
}if (retval != ERROR_OK) { ... }
}else if (target->state == TARGET_HALTED && !halted_before_profiling) { ... }
retval = target_poll(target);
if (retval != ERROR_OK) {
free(samples);
return retval;
}if (retval != ERROR_OK) { ... }
write_gmon(samples, num_of_samples, CMD_ARGV[1],
with_range, start_address, end_address, target, duration_ms);
command_print(CMD, "Wrote %s", CMD_ARGV[1]);
free(samples);
return retval;
}{ ... }
COMMAND_HANDLER(handle_target_read_memory)
{
/* ... */
if (CMD_ARGC < 3 || CMD_ARGC > 4)
return ERROR_COMMAND_SYNTAX_ERROR;
target_addr_t addr;
COMMAND_PARSE_NUMBER(u64, CMD_ARGV[0], addr);
unsigned int width_bits;
COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], width_bits);
unsigned int count;
COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
bool is_phys = false;
if (CMD_ARGC == 4) {
if (strcmp(CMD_ARGV[3], "phys")) {
command_print(CMD, "invalid argument '%s', must be 'phys'", CMD_ARGV[3]);
return ERROR_COMMAND_ARGUMENT_INVALID;
}if (strcmp(CMD_ARGV[3], "phys")) { ... }
is_phys = true;
}if (CMD_ARGC == 4) { ... }
switch (width_bits) {
case 8:
case 16:
case 32:
case 64:
break;case 64:
default:
command_print(CMD, "invalid width, must be 8, 16, 32 or 64");
return ERROR_COMMAND_ARGUMENT_INVALID;default
}switch (width_bits) { ... }
const unsigned int width = width_bits / 8;
if ((addr + (count * width)) < addr) {
command_print(CMD, "read_memory: addr + count wraps to zero");
return ERROR_COMMAND_ARGUMENT_INVALID;
}if ((addr + (count * width)) < addr) { ... }
if (count > 65536) {
command_print(CMD, "read_memory: too large read request, exceeds 64K elements");
return ERROR_COMMAND_ARGUMENT_INVALID;
}if (count > 65536) { ... }
struct target *target = get_current_target(CMD_CTX);
const size_t buffersize = 4096;
uint8_t *buffer = malloc(buffersize);
if (!buffer) {
LOG_ERROR("Failed to allocate memory");
return ERROR_FAIL;
}if (!buffer) { ... }
char *separator = "";
while (count > 0) {
const unsigned int max_chunk_len = buffersize / width;
const size_t chunk_len = MIN(count, max_chunk_len);
int retval;
if (is_phys)
retval = target_read_phys_memory(target, addr, width, chunk_len, buffer);
else
retval = target_read_memory(target, addr, width, chunk_len, buffer);
if (retval != ERROR_OK) {
LOG_DEBUG("read_memory: read at " TARGET_ADDR_FMT " with width=%u and count=%zu failed",
addr, width_bits, chunk_len);
/* ... */
command_print(CMD, "read_memory: failed to read memory");
free(buffer);
return retval;
}if (retval != ERROR_OK) { ... }
for (size_t i = 0; i < chunk_len ; i++) {
uint64_t v = 0;
switch (width) {
case 8:
v = target_buffer_get_u64(target, &buffer[i * width]);
break;case 8:
case 4:
v = target_buffer_get_u32(target, &buffer[i * width]);
break;case 4:
case 2:
v = target_buffer_get_u16(target, &buffer[i * width]);
break;case 2:
case 1:
v = buffer[i];
break;case 1:
}switch (width) { ... }
command_print_sameline(CMD, "%s0x%" PRIx64, separator, v);
separator = " ";
}for (size_t i = 0; i < chunk_len ; i++) { ... }
count -= chunk_len;
addr += chunk_len * width;
}while (count > 0) { ... }
free(buffer);
return ERROR_OK;
}{ ... }
static int target_jim_write_memory(Jim_Interp *interp, int argc,
Jim_Obj * const *argv)
{
/* ... */
if (argc < 4 || argc > 5) {
Jim_WrongNumArgs(interp, 1, argv, "address width data ['phys']");
return JIM_ERR;
}if (argc < 4 || argc > 5) { ... }
int e;
jim_wide wide_addr;
e = Jim_GetWide(interp, argv[1], &wide_addr);
if (e != JIM_OK)
return e;
target_addr_t addr = (target_addr_t)wide_addr;
long l;
e = Jim_GetLong(interp, argv[2], &l);
if (e != JIM_OK)
return e;
const unsigned int width_bits = l;
size_t count = Jim_ListLength(interp, argv[3]);
bool is_phys = false;
if (argc > 4) {
const char *phys = Jim_GetString(argv[4], NULL);
if (strcmp(phys, "phys")) {
Jim_SetResultFormatted(interp, "invalid argument '%s', must be 'phys'", phys);
return JIM_ERR;
}if (strcmp(phys, "phys")) { ... }
is_phys = true;
}if (argc > 4) { ... }
switch (width_bits) {
case 8:
case 16:
case 32:
case 64:
break;case 64:
default:
Jim_SetResultString(interp, "invalid width, must be 8, 16, 32 or 64", -1);
return JIM_ERR;default
}switch (width_bits) { ... }
const unsigned int width = width_bits / 8;
if ((addr + (count * width)) < addr) {
Jim_SetResultString(interp, "write_memory: addr + len wraps to zero", -1);
return JIM_ERR;
}if ((addr + (count * width)) < addr) { ... }
if (count > 65536) {
Jim_SetResultString(interp, "write_memory: too large memory write request, exceeds 64K elements", -1);
return JIM_ERR;
}if (count > 65536) { ... }
struct command_context *cmd_ctx = current_command_context(interp);
assert(cmd_ctx != NULL);
struct target *target = get_current_target(cmd_ctx);
const size_t buffersize = 4096;
uint8_t *buffer = malloc(buffersize);
if (!buffer) {
LOG_ERROR("Failed to allocate memory");
return JIM_ERR;
}if (!buffer) { ... }
size_t j = 0;
while (count > 0) {
const unsigned int max_chunk_len = buffersize / width;
const size_t chunk_len = MIN(count, max_chunk_len);
for (size_t i = 0; i < chunk_len; i++, j++) {
Jim_Obj *tmp = Jim_ListGetIndex(interp, argv[3], j);
jim_wide element_wide;
Jim_GetWide(interp, tmp, &element_wide);
const uint64_t v = element_wide;
switch (width) {
case 8:
target_buffer_set_u64(target, &buffer[i * width], v);
break;case 8:
case 4:
target_buffer_set_u32(target, &buffer[i * width], v);
break;case 4:
case 2:
target_buffer_set_u16(target, &buffer[i * width], v);
break;case 2:
case 1:
buffer[i] = v & 0x0ff;
break;case 1:
}switch (width) { ... }
}for (size_t i = 0; i < chunk_len; i++, j++) { ... }
count -= chunk_len;
int retval;
if (is_phys)
retval = target_write_phys_memory(target, addr, width, chunk_len, buffer);
else
retval = target_write_memory(target, addr, width, chunk_len, buffer);
if (retval != ERROR_OK) {
LOG_ERROR("write_memory: write at " TARGET_ADDR_FMT " with width=%u and count=%zu failed",
addr, width_bits, chunk_len);
Jim_SetResultString(interp, "write_memory: failed to write memory", -1);
e = JIM_ERR;
break;
}if (retval != ERROR_OK) { ... }
addr += chunk_len * width;
}while (count > 0) { ... }
free(buffer);
return e;
}{ ... }
/* ... */
void target_handle_event(struct target *target, enum target_event e)
{
struct target_event_action *teap;
int retval;
for (teap = target->event_action; teap; teap = teap->next) {
if (teap->event == e) {
LOG_DEBUG("target: %s (%s) event: %d (%s) action: %s",
target_name(target),
target_type_name(target),
e,
target_event_name(e),
Jim_GetString(teap->body, NULL));
/* ... */
struct command_context *cmd_ctx = current_command_context(teap->interp);
struct target *saved_target_override = cmd_ctx->current_target_override;
cmd_ctx->current_target_override = target;
if ((teap->event == TARGET_EVENT_GDB_FLASH_ERASE_START || teap->event == TARGET_EVENT_GDB_FLASH_WRITE_END) && target->first_reset)
retval = JIM_OK;
else
retval = Jim_EvalObj(teap->interp, teap->body);
cmd_ctx->current_target_override = saved_target_override;
if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
return;
if (retval == JIM_RETURN)
retval = teap->interp->returnCode;
if (retval != JIM_OK) {
Jim_MakeErrorMessage(teap->interp);
LOG_TARGET_ERROR(target, "Execution of event %s failed:\n%s",
target_event_name(e),
Jim_GetString(Jim_GetResult(teap->interp), NULL));
Jim_Eval(teap->interp, "error \"\" \"\"");
}if (retval != JIM_OK) { ... }
}if (teap->event == e) { ... }
}for (teap = target->event_action; teap; teap = teap->next) { ... }
}{ ... }
static int target_jim_get_reg(Jim_Interp *interp, int argc,
Jim_Obj * const *argv)
{
bool force = false;
if (argc == 3) {
const char *option = Jim_GetString(argv[1], NULL);
if (!strcmp(option, "-force")) {
argc--;
argv++;
force = true;
}if (!strcmp(option, "-force")) { ... } else {
Jim_SetResultFormatted(interp, "invalid option '%s'", option);
return JIM_ERR;
}else { ... }
}if (argc == 3) { ... }
if (argc != 2) {
Jim_WrongNumArgs(interp, 1, argv, "[-force] list");
return JIM_ERR;
}if (argc != 2) { ... }
const int length = Jim_ListLength(interp, argv[1]);
Jim_Obj *result_dict = Jim_NewDictObj(interp, NULL, 0);
if (!result_dict)
return JIM_ERR;
struct command_context *cmd_ctx = current_command_context(interp);
assert(cmd_ctx != NULL);
const struct target *target = get_current_target(cmd_ctx);
for (int i = 0; i < length; i++) {
Jim_Obj *elem = Jim_ListGetIndex(interp, argv[1], i);
if (!elem)
return JIM_ERR;
const char *reg_name = Jim_String(elem);
struct reg *reg = register_get_by_name(target->reg_cache, reg_name,
false);
if (!reg || !reg->exist) {
Jim_SetResultFormatted(interp, "unknown register '%s'", reg_name);
return JIM_ERR;
}if (!reg || !reg->exist) { ... }
if (force || !reg->valid) {
int retval = reg->type->get(reg);
if (retval != ERROR_OK) {
Jim_SetResultFormatted(interp, "failed to read register '%s'",
reg_name);
return JIM_ERR;
}if (retval != ERROR_OK) { ... }
}if (force || !reg->valid) { ... }
char *reg_value = buf_to_hex_str(reg->value, reg->size);
if (!reg_value) {
LOG_ERROR("Failed to allocate memory");
return JIM_ERR;
}if (!reg_value) { ... }
char *tmp = alloc_printf("0x%s", reg_value);
free(reg_value);
if (!tmp) {
LOG_ERROR("Failed to allocate memory");
return JIM_ERR;
}if (!tmp) { ... }
Jim_DictAddElement(interp, result_dict, elem,
Jim_NewStringObj(interp, tmp, -1));
free(tmp);
}for (int i = 0; i < length; i++) { ... }
Jim_SetResult(interp, result_dict);
return JIM_OK;
}{ ... }
COMMAND_HANDLER(handle_set_reg_command)
{
if (CMD_ARGC != 1)
return ERROR_COMMAND_SYNTAX_ERROR;
int tmp;
#if JIM_VERSION >= 80
Jim_Obj **dict = Jim_DictPairs(CMD_CTX->interp, CMD_JIMTCL_ARGV[0], &tmp);
if (!dict)
return ERROR_FAIL;/* ... */
#else
Jim_Obj **dict;
int ret = Jim_DictPairs(CMD_CTX->interp, CMD_JIMTCL_ARGV[0], &dict, &tmp);
if (ret != JIM_OK)
return ERROR_FAIL;/* ... */
#endif
const unsigned int length = tmp;
const struct target *target = get_current_target(CMD_CTX);
assert(target);
for (unsigned int i = 0; i < length; i += 2) {
const char *reg_name = Jim_String(dict[i]);
const char *reg_value = Jim_String(dict[i + 1]);
struct reg *reg = register_get_by_name(target->reg_cache, reg_name, false);
if (!reg || !reg->exist) {
command_print(CMD, "unknown register '%s'", reg_name);
return ERROR_FAIL;
}if (!reg || !reg->exist) { ... }
uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
if (!buf) {
LOG_ERROR("Failed to allocate memory");
return ERROR_FAIL;
}if (!buf) { ... }
int retval = CALL_COMMAND_HANDLER(command_parse_str_to_buf, reg_value, buf, reg->size);
if (retval != ERROR_OK) {
free(buf);
return retval;
}if (retval != ERROR_OK) { ... }
retval = reg->type->set(reg, buf);
free(buf);
if (retval != ERROR_OK) {
command_print(CMD, "failed to set '%s' to register '%s'",
reg_value, reg_name);
return retval;
}if (retval != ERROR_OK) { ... }
}for (unsigned int i = 0; i < length; i += 2) { ... }
return ERROR_OK;
}{ ... }
/* ... */
bool target_has_event_action(const struct target *target, enum target_event event)
{
struct target_event_action *teap;
for (teap = target->event_action; teap; teap = teap->next) {
if (teap->event == event)
return true;
}for (teap = target->event_action; teap; teap = teap->next) { ... }
return false;
}{ ... }
enum target_cfg_param {
TCFG_TYPE,
TCFG_EVENT,
TCFG_WORK_AREA_VIRT,
TCFG_WORK_AREA_PHYS,
TCFG_WORK_AREA_SIZE,
TCFG_WORK_AREA_BACKUP,
TCFG_ENDIAN,
TCFG_COREID,
TCFG_CHAIN_POSITION,
TCFG_DBGBASE,
TCFG_RTOS,
TCFG_DEFER_EXAMINE,
TCFG_GDB_PORT,
TCFG_GDB_MAX_CONNECTIONS,
...};
static struct jim_nvp nvp_config_opts[] = {
{ .name = "-type", .value = TCFG_TYPE },
{ .name = "-event", .value = TCFG_EVENT },
{ .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
{ .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
{ .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
{ .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
{ .name = "-endian", .value = TCFG_ENDIAN },
{ .name = "-coreid", .value = TCFG_COREID },
{ .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
{ .name = "-dbgbase", .value = TCFG_DBGBASE },
{ .name = "-rtos", .value = TCFG_RTOS },
{ .name = "-defer-examine", .value = TCFG_DEFER_EXAMINE },
{ .name = "-gdb-port", .value = TCFG_GDB_PORT },
{ .name = "-gdb-max-connections", .value = TCFG_GDB_MAX_CONNECTIONS },
{ .name = NULL, .value = -1 }
...};
static int target_configure(struct jim_getopt_info *goi, struct target *target)
{
struct jim_nvp *n;
Jim_Obj *o;
jim_wide w;
int e;
while (goi->argc > 0) {
Jim_SetEmptyResult(goi->interp);
if (target->type->target_jim_configure) {
e = (*(target->type->target_jim_configure))(target, goi);
if (e == JIM_OK) {
continue;
}if (e == JIM_OK) { ... }
if (e == JIM_ERR) {
return e;
}if (e == JIM_ERR) { ... }
}if (target->type->target_jim_configure) { ... }
e = jim_getopt_nvp(goi, nvp_config_opts, &n);
if (e != JIM_OK) {
jim_getopt_nvp_unknown(goi, nvp_config_opts, 0);
return e;
}if (e != JIM_OK) { ... }
switch (n->value) {
case TCFG_TYPE:
if (goi->isconfigure) {
Jim_SetResultFormatted(goi->interp,
"not settable: %s", n->name);
return JIM_ERR;
}if (goi->isconfigure) { ... } else {
no_params:
if (goi->argc != 0) {
Jim_WrongNumArgs(goi->interp,
goi->argc, goi->argv,
"NO PARAMS");
return JIM_ERR;
}if (goi->argc != 0) { ... }
}else { ... }
Jim_SetResultString(goi->interp,
target_type_name(target), -1);
break;case TCFG_TYPE:
case TCFG_EVENT:
if (goi->argc == 0) {
Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
return JIM_ERR;
}if (goi->argc == 0) { ... }
e = jim_getopt_nvp(goi, nvp_target_event, &n);
if (e != JIM_OK) {
jim_getopt_nvp_unknown(goi, nvp_target_event, 1);
return e;
}if (e != JIM_OK) { ... }
if (goi->isconfigure) {
if (goi->argc != 1) {
Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
return JIM_ERR;
}if (goi->argc != 1) { ... }
}if (goi->isconfigure) { ... } else {
if (goi->argc != 0) {
Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
return JIM_ERR;
}if (goi->argc != 0) { ... }
}else { ... }
{
struct target_event_action *teap;
teap = target->event_action;
while (teap) {
if (teap->event == (enum target_event)n->value)
break;
teap = teap->next;
}while (teap) { ... }
if (goi->isconfigure) {
if (n->value == TARGET_EVENT_TRACE_CONFIG)
LOG_INFO("DEPRECATED target event %s; use TPIU events {pre,post}-{enable,disable}", n->name);
bool replace = true;
if (!teap) {
teap = calloc(1, sizeof(*teap));
replace = false;
}if (!teap) { ... }
teap->event = n->value;
teap->interp = goi->interp;
jim_getopt_obj(goi, &o);
if (teap->body)
Jim_DecrRefCount(teap->interp, teap->body);
teap->body = Jim_DuplicateObj(goi->interp, o);
/* ... */
Jim_IncrRefCount(teap->body);
if (!replace) {
teap->next = target->event_action;
target->event_action = teap;
}if (!replace) { ... }
Jim_SetEmptyResult(goi->interp);
}if (goi->isconfigure) { ... } else {
if (!teap)
Jim_SetEmptyResult(goi->interp);
else
Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
}else { ... }
...}
break;
case TCFG_EVENT:
case TCFG_WORK_AREA_VIRT:
if (goi->isconfigure) {
target_free_all_working_areas(target);
e = jim_getopt_wide(goi, &w);
if (e != JIM_OK)
return e;
target->working_area_virt = w;
target->working_area_virt_spec = true;
}if (goi->isconfigure) { ... } else {
if (goi->argc != 0)
goto no_params;
}else { ... }
Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
break;
case TCFG_WORK_AREA_VIRT:
case TCFG_WORK_AREA_PHYS:
if (goi->isconfigure) {
target_free_all_working_areas(target);
e = jim_getopt_wide(goi, &w);
if (e != JIM_OK)
return e;
target->working_area_phys = w;
target->working_area_phys_spec = true;
}if (goi->isconfigure) { ... } else {
if (goi->argc != 0)
goto no_params;
}else { ... }
Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
break;
case TCFG_WORK_AREA_PHYS:
case TCFG_WORK_AREA_SIZE:
if (goi->isconfigure) {
target_free_all_working_areas(target);
e = jim_getopt_wide(goi, &w);
if (e != JIM_OK)
return e;
target->working_area_size = w;
}if (goi->isconfigure) { ... } else {
if (goi->argc != 0)
goto no_params;
}else { ... }
Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
break;
case TCFG_WORK_AREA_SIZE:
case TCFG_WORK_AREA_BACKUP:
if (goi->isconfigure) {
target_free_all_working_areas(target);
e = jim_getopt_wide(goi, &w);
if (e != JIM_OK)
return e;
target->backup_working_area = (w != 0);
}if (goi->isconfigure) { ... } else {
if (goi->argc != 0)
goto no_params;
}else { ... }
Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area ? 1 : 0));
break;
case TCFG_WORK_AREA_BACKUP:
case TCFG_ENDIAN:
if (goi->isconfigure) {
e = jim_getopt_nvp(goi, nvp_target_endian, &n);
if (e != JIM_OK) {
jim_getopt_nvp_unknown(goi, nvp_target_endian, 1);
return e;
}if (e != JIM_OK) { ... }
target->endianness = n->value;
}if (goi->isconfigure) { ... } else {
if (goi->argc != 0)
goto no_params;
}else { ... }
n = jim_nvp_value2name_simple(nvp_target_endian, target->endianness);
if (!n->name) {
target->endianness = TARGET_LITTLE_ENDIAN;
n = jim_nvp_value2name_simple(nvp_target_endian, target->endianness);
}if (!n->name) { ... }
Jim_SetResultString(goi->interp, n->name, -1);
break;
case TCFG_ENDIAN:
case TCFG_COREID:
if (goi->isconfigure) {
e = jim_getopt_wide(goi, &w);
if (e != JIM_OK)
return e;
target->coreid = (int32_t)w;
}if (goi->isconfigure) { ... } else {
if (goi->argc != 0)
goto no_params;
}else { ... }
Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->coreid));
break;
case TCFG_COREID:
case TCFG_CHAIN_POSITION:
if (goi->isconfigure) {
Jim_Obj *o_t;
struct jtag_tap *tap;
if (target->has_dap) {
Jim_SetResultString(goi->interp,
"target requires -dap parameter instead of -chain-position, see http://visualgdb.com/support/chainposition", -1);
return JIM_ERR;
}if (target->has_dap) { ... }
target_free_all_working_areas(target);
e = jim_getopt_obj(goi, &o_t);
if (e != JIM_OK)
return e;
tap = jtag_tap_by_jim_obj(goi->interp, o_t);
if (!tap)
return JIM_ERR;
target->tap = tap;
target->tap_configured = true;
}if (goi->isconfigure) { ... } else {
if (goi->argc != 0)
goto no_params;
}else { ... }
Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
break;case TCFG_CHAIN_POSITION:
case TCFG_DBGBASE:
if (goi->isconfigure) {
e = jim_getopt_wide(goi, &w);
if (e != JIM_OK)
return e;
target->dbgbase = (uint32_t)w;
target->dbgbase_set = true;
}if (goi->isconfigure) { ... } else {
if (goi->argc != 0)
goto no_params;
}else { ... }
Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->dbgbase));
break;case TCFG_DBGBASE:
case TCFG_RTOS:
{
int result = rtos_create(goi, target);
if (result != JIM_OK)
return result;
...}
break;
case TCFG_RTOS:
case TCFG_DEFER_EXAMINE:
target->defer_examine = true;
break;
case TCFG_DEFER_EXAMINE:
case TCFG_GDB_PORT:
if (goi->isconfigure) {
struct command_context *cmd_ctx = current_command_context(goi->interp);
if (cmd_ctx->mode != COMMAND_CONFIG) {
Jim_SetResultString(goi->interp, "-gdb-port must be configured before 'init'", -1);
return JIM_ERR;
}if (cmd_ctx->mode != COMMAND_CONFIG) { ... }
const char *s;
e = jim_getopt_string(goi, &s, NULL);
if (e != JIM_OK)
return e;
free(target->gdb_port_override);
target->gdb_port_override = strdup(s);
}if (goi->isconfigure) { ... } else {
if (goi->argc != 0)
goto no_params;
}else { ... }
Jim_SetResultString(goi->interp, target->gdb_port_override ? target->gdb_port_override : "undefined", -1);
break;
case TCFG_GDB_PORT:
case TCFG_GDB_MAX_CONNECTIONS:
if (goi->isconfigure) {
struct command_context *cmd_ctx = current_command_context(goi->interp);
if (cmd_ctx->mode != COMMAND_CONFIG) {
Jim_SetResultString(goi->interp, "-gdb-max-connections must be configured before 'init'", -1);
return JIM_ERR;
}if (cmd_ctx->mode != COMMAND_CONFIG) { ... }
e = jim_getopt_wide(goi, &w);
if (e != JIM_OK)
return e;
target->gdb_max_connections = (w < 0) ? CONNECTION_LIMIT_UNLIMITED : (int)w;
}if (goi->isconfigure) { ... } else {
if (goi->argc != 0)
goto no_params;
}else { ... }
Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->gdb_max_connections));
break;case TCFG_GDB_MAX_CONNECTIONS:
}switch (n->value) { ... }
}while (goi->argc > 0) { ... }
return JIM_OK;
}{ ... }
static int jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
{
struct command *c = jim_to_command(interp);
struct jim_getopt_info goi;
jim_getopt_setup(&goi, interp, argc - 1, argv + 1);
goi.isconfigure = !strcmp(c->name, "configure");
if (goi.argc < 1) {
Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
"missing: -option ...");
return JIM_ERR;
}if (goi.argc < 1) { ... }
struct command_context *cmd_ctx = current_command_context(interp);
assert(cmd_ctx);
struct target *target = get_current_target(cmd_ctx);
return target_configure(&goi, target);
}{ ... }
COMMAND_HANDLER(handle_target_examine)
{
bool allow_defer = false;
if (CMD_ARGC > 1)
return ERROR_COMMAND_SYNTAX_ERROR;
if (CMD_ARGC == 1) {
if (strcmp(CMD_ARGV[0], "allow-defer"))
return ERROR_COMMAND_ARGUMENT_INVALID;
allow_defer = true;
}if (CMD_ARGC == 1) { ... }
struct target *target = get_current_target(CMD_CTX);
if (!target->tap->enabled) {
command_print(CMD, "[TAP is disabled]");
return ERROR_FAIL;
}if (!target->tap->enabled) { ... }
if (allow_defer && target->defer_examine) {
LOG_INFO("Deferring arp_examine of %s", target_name(target));
LOG_INFO("Use arp_examine command to examine it manually!");
return ERROR_OK;
}if (allow_defer && target->defer_examine) { ... }
int retval = target->type->examine(target);
if (retval != ERROR_OK) {
target_reset_examined(target);
return retval;
}if (retval != ERROR_OK) { ... }
target_set_examined(target);
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(handle_target_was_examined)
{
if (CMD_ARGC != 0)
return ERROR_COMMAND_SYNTAX_ERROR;
struct target *target = get_current_target(CMD_CTX);
command_print(CMD, "%d", target_was_examined(target) ? 1 : 0);
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(handle_target_examine_deferred)
{
if (CMD_ARGC != 0)
return ERROR_COMMAND_SYNTAX_ERROR;
struct target *target = get_current_target(CMD_CTX);
command_print(CMD, "%d", target->defer_examine ? 1 : 0);
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(handle_target_halt_gdb)
{
if (CMD_ARGC != 0)
return ERROR_COMMAND_SYNTAX_ERROR;
struct target *target = get_current_target(CMD_CTX);
return target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
}{ ... }
COMMAND_HANDLER(handle_target_poll)
{
if (CMD_ARGC != 0)
return ERROR_COMMAND_SYNTAX_ERROR;
struct target *target = get_current_target(CMD_CTX);
if (!target->tap->enabled) {
command_print(CMD, "[TAP is disabled]");
return ERROR_FAIL;
}if (!target->tap->enabled) { ... }
if (!(target_was_examined(target)))
return ERROR_TARGET_NOT_EXAMINED;
return target->type->poll(target);
}{ ... }
COMMAND_HANDLER(handle_target_reset)
{
if (CMD_ARGC != 2)
return ERROR_COMMAND_SYNTAX_ERROR;
const struct nvp *n = nvp_name2value(nvp_assert, CMD_ARGV[0]);
if (!n->name) {
nvp_unknown_command_print(CMD, nvp_assert, NULL, CMD_ARGV[0]);
return ERROR_COMMAND_ARGUMENT_INVALID;
}if (!n->name) { ... }
int a;
COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], a);
struct target *target = get_current_target(CMD_CTX);
if (!target->tap->enabled) {
command_print(CMD, "[TAP is disabled]");
return ERROR_FAIL;
}if (!target->tap->enabled) { ... }
if (!target->type->assert_reset || !target->type->deassert_reset) {
command_print(CMD, "No target-specific reset for %s", target_name(target));
return ERROR_FAIL;
}if (!target->type->assert_reset || !target->type->deassert_reset) { ... }
target->reset_halt = (a != 0);
target_free_all_working_areas_restore(target, 0);
if (n->value == NVP_ASSERT) {
int retval = target->type->assert_reset(target);
if (target->defer_examine)
target_reset_examined(target);
return retval;
}if (n->value == NVP_ASSERT) { ... }
return target->type->deassert_reset(target);
}{ ... }
COMMAND_HANDLER(handle_target_halt)
{
if (CMD_ARGC != 0)
return ERROR_COMMAND_SYNTAX_ERROR;
struct target *target = get_current_target(CMD_CTX);
if (!target->tap->enabled) {
command_print(CMD, "[TAP is disabled]");
return ERROR_FAIL;
}if (!target->tap->enabled) { ... }
return target->type->halt(target);
}{ ... }
COMMAND_HANDLER(handle_target_wait_state)
{
if (CMD_ARGC != 2)
return ERROR_COMMAND_SYNTAX_ERROR;
const struct nvp *n = nvp_name2value(nvp_target_state, CMD_ARGV[0]);
if (!n->name) {
nvp_unknown_command_print(CMD, nvp_target_state, NULL, CMD_ARGV[0]);
return ERROR_COMMAND_ARGUMENT_INVALID;
}if (!n->name) { ... }
unsigned int a;
COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], a);
struct target *target = get_current_target(CMD_CTX);
if (!target->tap->enabled) {
command_print(CMD, "[TAP is disabled]");
return ERROR_FAIL;
}if (!target->tap->enabled) { ... }
int retval = target_wait_state(target, n->value, a);
if (retval != ERROR_OK) {
command_print(CMD,
"target: %s wait %s fails (%d) %s",
target_name(target), n->name,
retval, target_strerror_safe(retval));
return retval;
}if (retval != ERROR_OK) { ... }
return ERROR_OK;
}{ ... }
/* ... */
COMMAND_HANDLER(handle_target_event_list)
{
struct target *target = get_current_target(CMD_CTX);
struct target_event_action *teap = target->event_action;
command_print(CMD, "Event actions for target %s\n",
target_name(target));
command_print(CMD, "%-25s | Body", "Event");
command_print(CMD, "------------------------- | "
"----------------------------------------");
while (teap) {
command_print(CMD, "%-25s | %s",
target_event_name(teap->event),
Jim_GetString(teap->body, NULL));
teap = teap->next;
}while (teap) { ... }
command_print(CMD, "***END***");
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(handle_target_current_state)
{
if (CMD_ARGC != 0)
return ERROR_COMMAND_SYNTAX_ERROR;
struct target *target = get_current_target(CMD_CTX);
command_print(CMD, "%s", target_state_name(target));
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(handle_target_debug_reason)
{
if (CMD_ARGC != 0)
return ERROR_COMMAND_SYNTAX_ERROR;
struct target *target = get_current_target(CMD_CTX);
const char *debug_reason = nvp_value2name(nvp_target_debug_reason,
target->debug_reason)->name;
if (!debug_reason) {
command_print(CMD, "bug: invalid debug reason (%d)",
target->debug_reason);
return ERROR_FAIL;
}if (!debug_reason) { ... }
command_print(CMD, "%s", debug_reason);
return ERROR_OK;
}{ ... }
static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
struct jim_getopt_info goi;
jim_getopt_setup(&goi, interp, argc - 1, argv + 1);
if (goi.argc != 1) {
const char *cmd_name = Jim_GetString(argv[0], NULL);
Jim_SetResultFormatted(goi.interp, "%s <eventname>", cmd_name);
return JIM_ERR;
}if (goi.argc != 1) { ... }
struct jim_nvp *n;
int e = jim_getopt_nvp(&goi, nvp_target_event, &n);
if (e != JIM_OK) {
jim_getopt_nvp_unknown(&goi, nvp_target_event, 1);
return e;
}if (e != JIM_OK) { ... }
struct command_context *cmd_ctx = current_command_context(interp);
assert(cmd_ctx);
struct target *target = get_current_target(cmd_ctx);
target_handle_event(target, n->value);
return JIM_OK;
}{ ... }
static const struct command_registration target_instance_command_handlers[] = {
{
.name = "configure",
.mode = COMMAND_ANY,
.jim_handler = jim_target_configure,
.help = "configure a new target for use",
.usage = "[target_attribute ...]",
...},
{
.name = "cget",
.mode = COMMAND_ANY,
.jim_handler = jim_target_configure,
.help = "returns the specified target attribute",
.usage = "target_attribute",
...},
{
.name = "mwd",
.handler = handle_mw_command,
.mode = COMMAND_EXEC,
.help = "Write 64-bit word(s) to target memory",
.usage = "address data [count]",
...},
{
.name = "mww",
.handler = handle_mw_command,
.mode = COMMAND_EXEC,
.help = "Write 32-bit word(s) to target memory",
.usage = "address data [count]",
...},
{
.name = "mwh",
.handler = handle_mw_command,
.mode = COMMAND_EXEC,
.help = "Write 16-bit half-word(s) to target memory",
.usage = "address data [count]",
...},
{
.name = "mwb",
.handler = handle_mw_command,
.mode = COMMAND_EXEC,
.help = "Write byte(s) to target memory",
.usage = "address data [count]",
...},
{
.name = "mdd",
.handler = handle_md_command,
.mode = COMMAND_EXEC,
.help = "Display target memory as 64-bit words",
.usage = "address [count]",
...},
{
.name = "mdw",
.handler = handle_md_command,
.mode = COMMAND_EXEC,
.help = "Display target memory as 32-bit words",
.usage = "address [count]",
...},
{
.name = "mdh",
.handler = handle_md_command,
.mode = COMMAND_EXEC,
.help = "Display target memory as 16-bit half-words",
.usage = "address [count]",
...},
{
.name = "mdb",
.handler = handle_md_command,
.mode = COMMAND_EXEC,
.help = "Display target memory as 8-bit bytes",
.usage = "address [count]",
...},
{
.name = "get_reg",
.mode = COMMAND_EXEC,
.jim_handler = target_jim_get_reg,
.help = "Get register values from the target",
.usage = "list",
...},
{
.name = "set_reg",
.mode = COMMAND_EXEC,
.handler = handle_set_reg_command,
.help = "Set target register values",
.usage = "dict",
...},
{
.name = "read_memory",
.mode = COMMAND_EXEC,
.handler = handle_target_read_memory,
.help = "Read Tcl list of 8/16/32/64 bit numbers from target memory",
.usage = "address width count ['phys']",
...},
{
.name = "write_memory",
.mode = COMMAND_EXEC,
.jim_handler = target_jim_write_memory,
.help = "Write Tcl list of 8/16/32/64 bit numbers to target memory",
.usage = "address width data ['phys']",
...},
{
.name = "eventlist",
.handler = handle_target_event_list,
.mode = COMMAND_EXEC,
.help = "displays a table of events defined for this target",
.usage = "",
...},
{
.name = "curstate",
.mode = COMMAND_EXEC,
.handler = handle_target_current_state,
.help = "displays the current state of this target",
.usage = "",
...},
{
.name = "debug_reason",
.mode = COMMAND_EXEC,
.handler = handle_target_debug_reason,
.help = "displays the debug reason of this target",
.usage = "",
...},
{
.name = "arp_examine",
.mode = COMMAND_EXEC,
.handler = handle_target_examine,
.help = "used internally for reset processing",
.usage = "['allow-defer']",
...},
{
.name = "was_examined",
.mode = COMMAND_EXEC,
.handler = handle_target_was_examined,
.help = "used internally for reset processing",
.usage = "",
...},
{
.name = "examine_deferred",
.mode = COMMAND_EXEC,
.handler = handle_target_examine_deferred,
.help = "used internally for reset processing",
.usage = "",
...},
{
.name = "arp_halt_gdb",
.mode = COMMAND_EXEC,
.handler = handle_target_halt_gdb,
.help = "used internally for reset processing to halt GDB",
.usage = "",
...},
{
.name = "arp_poll",
.mode = COMMAND_EXEC,
.handler = handle_target_poll,
.help = "used internally for reset processing",
.usage = "",
...},
{
.name = "arp_reset",
.mode = COMMAND_EXEC,
.handler = handle_target_reset,
.help = "used internally for reset processing",
.usage = "'assert'|'deassert' halt",
...},
{
.name = "arp_halt",
.mode = COMMAND_EXEC,
.handler = handle_target_halt,
.help = "used internally for reset processing",
.usage = "",
...},
{
.name = "arp_waitstate",
.mode = COMMAND_EXEC,
.handler = handle_target_wait_state,
.help = "used internally for reset processing",
.usage = "statename timeoutmsecs",
...},
{
.name = "invoke-event",
.mode = COMMAND_EXEC,
.jim_handler = jim_target_invoke_event,
.help = "invoke handler for specified event",
.usage = "event_name",
...},
COMMAND_REGISTRATION_DONE
...};
static int target_create(struct jim_getopt_info *goi)
{
Jim_Obj *new_cmd;
Jim_Cmd *cmd;
const char *cp;
int e;
int x;
struct target *target;
struct command_context *cmd_ctx;
cmd_ctx = current_command_context(goi->interp);
assert(cmd_ctx);
if (goi->argc < 3) {
Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
return JIM_ERR;
}if (goi->argc < 3) { ... }
jim_getopt_obj(goi, &new_cmd);
cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_NONE);
if (cmd) {
cp = Jim_GetString(new_cmd, NULL);
Jim_SetResultFormatted(goi->interp, "Command/target: %s Exists", cp);
return JIM_ERR;
}if (cmd) { ... }
e = jim_getopt_string(goi, &cp, NULL);
if (e != JIM_OK)
return e;
struct transport *tr = get_current_transport();
if (tr && tr->override_target) {
e = tr->override_target(&cp);
if (e != ERROR_OK) {
LOG_ERROR("The selected transport doesn't support this target");
return JIM_ERR;
}if (e != ERROR_OK) { ... }
LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
}if (tr && tr->override_target) { ... }
for (x = 0 ; target_types[x] ; x++) {
if (strcmp(cp, target_types[x]->name) == 0) {
break;
}if (strcmp(cp, target_types[x]->name) == 0) { ... }
}for (x = 0 ; target_types[x] ; x++) { ... }
if (!target_types[x]) {
Jim_SetResultFormatted(goi->interp, "Unknown target type %s, try one of ", cp);
for (x = 0 ; target_types[x] ; x++) {
if (target_types[x + 1]) {
Jim_AppendStrings(goi->interp,
Jim_GetResult(goi->interp),
target_types[x]->name,
", ", NULL);
}if (target_types[x + 1]) { ... } else {
Jim_AppendStrings(goi->interp,
Jim_GetResult(goi->interp),
" or ",
target_types[x]->name, NULL);
}else { ... }
}for (x = 0 ; target_types[x] ; x++) { ... }
return JIM_ERR;
}if (!target_types[x]) { ... }
target = calloc(1, sizeof(struct target));
if (!target) {
LOG_ERROR("Out of memory");
return JIM_ERR;
}if (!target) { ... }
target->smp_targets = &empty_smp_targets;
target->type = malloc(sizeof(struct target_type));
if (!target->type) {
LOG_ERROR("Out of memory");
free(target);
return JIM_ERR;
}if (!target->type) { ... }
memcpy(target->type, target_types[x], sizeof(struct target_type));
target->coreid = 0;
target->working_area = 0x0;
target->working_area_size = 0x0;
target->working_areas = NULL;
target->backup_working_area = false;
target->state = TARGET_UNKNOWN;
target->debug_reason = DBG_REASON_UNDEFINED;
target->reg_cache = NULL;
target->breakpoints = NULL;
target->watchpoints = NULL;
target->next = NULL;
target->arch_info = NULL;
target->verbose_halt_msg = true;
target->halt_issued = false;
target->trace_info = calloc(1, sizeof(struct trace));
if (!target->trace_info) {
LOG_ERROR("Out of memory");
free(target->type);
free(target);
return JIM_ERR;
}if (!target->trace_info) { ... }
target->dbgmsg = NULL;
target->dbg_msg_enabled = false;
target->endianness = TARGET_ENDIAN_UNKNOWN;
target->rtos = NULL;
target->rtos_auto_detect = false;
target->gdb_port_override = NULL;
target->gdb_max_connections = 1;
goi->isconfigure = 1;
e = target_configure(goi, target);
if (e == JIM_OK) {
if (target->has_dap) {
if (!target->dap_configured) {
Jim_SetResultString(goi->interp, "-dap ?name? required when creating target", -1);
e = JIM_ERR;
}if (!target->dap_configured) { ... }
}if (target->has_dap) { ... } else {
if (!target->tap_configured) {
Jim_SetResultString(goi->interp, "-chain-position ?name? required when creating target", -1);
e = JIM_ERR;
}if (!target->tap_configured) { ... }
}else { ... }
if (!target->tap)
e = JIM_ERR;
}if (e == JIM_OK) { ... }
if (e != JIM_OK) {
rtos_destroy(target);
free(target->gdb_port_override);
free(target->trace_info);
free(target->type);
free(target);
return e;
}if (e != JIM_OK) { ... }
if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
target->endianness = TARGET_LITTLE_ENDIAN;
}if (target->endianness == TARGET_ENDIAN_UNKNOWN) { ... }
cp = Jim_GetString(new_cmd, NULL);
target->cmd_name = strdup(cp);
if (!target->cmd_name) {
LOG_ERROR("Out of memory");
rtos_destroy(target);
free(target->gdb_port_override);
free(target->trace_info);
free(target->type);
free(target);
return JIM_ERR;
}if (!target->cmd_name) { ... }
if (target->type->target_create) {
e = (*(target->type->target_create))(target, goi->interp);
if (e != ERROR_OK) {
LOG_DEBUG("target_create failed");
free(target->cmd_name);
rtos_destroy(target);
free(target->gdb_port_override);
free(target->trace_info);
free(target->type);
free(target);
return JIM_ERR;
}if (e != ERROR_OK) { ... }
}if (target->type->target_create) { ... }
if (target->type->commands) {
e = register_commands(cmd_ctx, NULL, target->type->commands);
if (e != ERROR_OK)
LOG_ERROR("unable to register '%s' commands", cp);
}if (target->type->commands) { ... }
const struct command_registration target_subcommands[] = {
{
.chain = target_instance_command_handlers,
...},
{
.chain = target->type->commands,
...},
COMMAND_REGISTRATION_DONE
...};
const struct command_registration target_commands[] = {
{
.name = cp,
.mode = COMMAND_ANY,
.help = "target command group",
.usage = "",
.chain = target_subcommands,
...},
COMMAND_REGISTRATION_DONE
...};
e = register_commands_override_target(cmd_ctx, NULL, target_commands, target);
if (e != ERROR_OK) {
if (target->type->deinit_target)
target->type->deinit_target(target);
free(target->cmd_name);
rtos_destroy(target);
free(target->gdb_port_override);
free(target->trace_info);
free(target->type);
free(target);
return JIM_ERR;
}if (e != ERROR_OK) { ... }
append_to_list_all_targets(target);
cmd_ctx->current_target = target;
return JIM_OK;
}{ ... }
COMMAND_HANDLER(handle_target_current)
{
if (CMD_ARGC != 0)
return ERROR_COMMAND_SYNTAX_ERROR;
struct target *target = get_current_target_or_null(CMD_CTX);
if (target)
command_print(CMD, "%s", target_name(target));
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(handle_target_types)
{
if (CMD_ARGC != 0)
return ERROR_COMMAND_SYNTAX_ERROR;
for (unsigned int x = 0; target_types[x]; x++)
command_print(CMD, "%s", target_types[x]->name);
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(handle_target_names)
{
if (CMD_ARGC != 0)
return ERROR_COMMAND_SYNTAX_ERROR;
struct target *target = all_targets;
while (target) {
command_print(CMD, "%s", target_name(target));
target = target->next;
}while (target) { ... }
return ERROR_OK;
}{ ... }
static struct target_list *
__attribute__((warn_unused_result))
create_target_list_node(const char *targetname)
{
struct target *target = get_target(targetname);
LOG_DEBUG("%s ", targetname);
if (!target)
return NULL;
struct target_list *new = malloc(sizeof(struct target_list));
if (!new) {
LOG_ERROR("Out of memory");
return new;
}if (!new) { ... }
new->target = target;
return new;
}{ ... }
static int get_target_with_common_rtos_type(struct command_invocation *cmd,
struct list_head *lh, struct target **result)
{
struct target *target = NULL;
struct target_list *curr;
foreach_smp_target(curr, lh) {
struct rtos *curr_rtos = curr->target->rtos;
if (curr_rtos) {
if (target && target->rtos && target->rtos->type != curr_rtos->type) {
command_print(cmd, "Different rtos types in members of one smp target!");
return ERROR_FAIL;
}if (target && target->rtos && target->rtos->type != curr_rtos->type) { ... }
target = curr->target;
}if (curr_rtos) { ... }
}foreach_smp_target (curr, lh) { ... }
*result = target;
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(handle_target_smp)
{
static int smp_group = 1;
if (CMD_ARGC == 0) {
LOG_DEBUG("Empty SMP target");
return ERROR_OK;
}if (CMD_ARGC == 0) { ... }
LOG_DEBUG("%d", CMD_ARGC);
/* ... */
struct list_head *lh = malloc(sizeof(*lh));
if (!lh) {
LOG_ERROR("Out of memory");
return ERROR_FAIL;
}if (!lh) { ... }
INIT_LIST_HEAD(lh);
for (unsigned int i = 0; i < CMD_ARGC; i++) {
struct target_list *new = create_target_list_node(CMD_ARGV[i]);
if (new)
list_add_tail(&new->lh, lh);
}for (unsigned int i = 0; i < CMD_ARGC; i++) { ... }
struct target_list *curr;
foreach_smp_target(curr, lh) {
struct target *target = curr->target;
target->smp = smp_group;
target->smp_targets = lh;
}foreach_smp_target (curr, lh) { ... }
smp_group++;
struct target *rtos_target;
int retval = get_target_with_common_rtos_type(CMD, lh, &rtos_target);
if (retval == ERROR_OK && rtos_target)
retval = rtos_smp_init(rtos_target);
return retval;
}{ ... }
static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
struct jim_getopt_info goi;
jim_getopt_setup(&goi, interp, argc - 1, argv + 1);
if (goi.argc < 3) {
Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
"<name> <target_type> [<target_options> ...]");
return JIM_ERR;
}if (goi.argc < 3) { ... }
return target_create(&goi);
}{ ... }
static const struct command_registration target_subcommand_handlers[] = {
{
.name = "init",
.mode = COMMAND_CONFIG,
.handler = handle_target_init_command,
.help = "initialize targets",
.usage = "",
...},
{
.name = "create",
.mode = COMMAND_CONFIG,
.jim_handler = jim_target_create,
.usage = "name type '-chain-position' name [options ...]",
.help = "Creates and selects a new target",
...},
{
.name = "current",
.mode = COMMAND_ANY,
.handler = handle_target_current,
.help = "Returns the currently selected target",
.usage = "",
...},
{
.name = "types",
.mode = COMMAND_ANY,
.handler = handle_target_types,
.help = "Returns the available target types as "
"a list of strings",
.usage = "",
...},
{
.name = "names",
.mode = COMMAND_ANY,
.handler = handle_target_names,
.help = "Returns the names of all targets as a list of strings",
.usage = "",
...},
{
.name = "smp",
.mode = COMMAND_ANY,
.handler = handle_target_smp,
.usage = "targetname1 targetname2 ...",
.help = "gather several target in a smp list"
...},
COMMAND_REGISTRATION_DONE
...};
struct fast_load {
target_addr_t address;
uint8_t *data;
int length;
...};
static int fastload_num;
static struct fast_load *fastload;
static void free_fastload(void)
{
if (fastload) {
for (int i = 0; i < fastload_num; i++)
free(fastload[i].data);
free(fastload);
fastload = NULL;
}if (fastload) { ... }
}{ ... }
COMMAND_HANDLER(handle_fast_load_image_command)
{
uint8_t *buffer;
size_t buf_cnt;
uint32_t image_size;
target_addr_t min_address = 0;
target_addr_t max_address = -1;
struct image image;
int retval = CALL_COMMAND_HANDLER(parse_load_image_command,
&image, &min_address, &max_address);
if (retval != ERROR_OK)
return retval;
struct duration bench;
duration_start(&bench);
retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL);
if (retval != ERROR_OK)
return retval;
image_size = 0x0;
retval = ERROR_OK;
fastload_num = image.num_sections;
fastload = malloc(sizeof(struct fast_load)*image.num_sections);
if (!fastload) {
command_print(CMD, "out of memory");
image_close(&image);
return ERROR_FAIL;
}if (!fastload) { ... }
memset(fastload, 0, sizeof(struct fast_load)*image.num_sections);
for (unsigned int i = 0; i < image.num_sections; i++) {
buffer = malloc(image.sections[i].size);
if (!buffer) {
command_print(CMD, "error allocating buffer for section (%d bytes)",
(int)(image.sections[i].size));
retval = ERROR_FAIL;
break;
}if (!buffer) { ... }
retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
if (retval != ERROR_OK) {
free(buffer);
break;
}if (retval != ERROR_OK) { ... }
uint32_t offset = 0;
uint32_t length = buf_cnt;
if ((image.sections[i].base_address + buf_cnt >= min_address) &&
(image.sections[i].base_address < max_address)) {
if (image.sections[i].base_address < min_address) {
offset += min_address-image.sections[i].base_address;
length -= offset;
}if (image.sections[i].base_address < min_address) { ... }
if (image.sections[i].base_address + buf_cnt > max_address)
length -= (image.sections[i].base_address + buf_cnt)-max_address;
fastload[i].address = image.sections[i].base_address + offset;
fastload[i].data = malloc(length);
if (!fastload[i].data) {
free(buffer);
command_print(CMD, "error allocating buffer for section (%" PRIu32 " bytes)",
length);
retval = ERROR_FAIL;
break;
}if (!fastload[i].data) { ... }
memcpy(fastload[i].data, buffer + offset, length);
fastload[i].length = length;
image_size += length;
command_print(CMD, "%u bytes written at address 0x%8.8x",
(unsigned int)length,
((unsigned int)(image.sections[i].base_address + offset)));
}if ((image.sections[i].base_address + buf_cnt >= min_address) && (image.sections[i].base_address < max_address)) { ... }
free(buffer);
}for (unsigned int i = 0; i < image.num_sections; i++) { ... }
if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
command_print(CMD, "Loaded %" PRIu32 " bytes "
"in %fs (%0.3f KiB/s)", image_size,
duration_elapsed(&bench), duration_kbps(&bench, image_size));
command_print(CMD,
"WARNING: image has not been loaded to target!"
"You can issue a 'fast_load' to finish loading.");
}if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) { ... }
image_close(&image);
if (retval != ERROR_OK)
free_fastload();
return retval;
}{ ... }
COMMAND_HANDLER(handle_fast_load_command)
{
if (CMD_ARGC > 0)
return ERROR_COMMAND_SYNTAX_ERROR;
if (!fastload) {
LOG_ERROR("No image in memory");
return ERROR_FAIL;
}if (!fastload) { ... }
int i;
int64_t ms = timeval_ms();
int size = 0;
int retval = ERROR_OK;
for (i = 0; i < fastload_num; i++) {
struct target *target = get_current_target(CMD_CTX);
command_print(CMD, "Write to 0x%08x, length 0x%08x",
(unsigned int)(fastload[i].address),
(unsigned int)(fastload[i].length));
retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
if (retval != ERROR_OK)
break;
size += fastload[i].length;
}for (i = 0; i < fastload_num; i++) { ... }
if (retval == ERROR_OK) {
int64_t after = timeval_ms();
command_print(CMD, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
}if (retval == ERROR_OK) { ... }
return retval;
}{ ... }
static const struct command_registration target_command_handlers[] = {
{
.name = "targets",
.handler = handle_targets_command,
.mode = COMMAND_ANY,
.help = "change current default target (one parameter) "
"or prints table of all targets (no parameters)",
.usage = "[target]",
...},
{
.name = "target",
.mode = COMMAND_CONFIG,
.help = "configure target",
.chain = target_subcommand_handlers,
.usage = "",
...},
COMMAND_REGISTRATION_DONE
...};
int target_register_commands(struct command_context *cmd_ctx)
{
return register_commands(cmd_ctx, NULL, target_command_handlers);
}{ ... }
static bool target_reset_nag = true;
bool get_target_reset_nag(void)
{
return target_reset_nag;
}{ ... }
COMMAND_HANDLER(handle_target_reset_nag)
{
return CALL_COMMAND_HANDLER(handle_command_parse_bool,
&target_reset_nag, "Nag after each reset about options to improve "
"performance");
}{ ... }
COMMAND_HANDLER(handle_ps_command)
{
struct target *target = get_current_target(CMD_CTX);
char *display;
if (target->state != TARGET_HALTED) {
command_print(CMD, "Error: [%s] not halted", target_name(target));
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
if ((target->rtos) && (target->rtos->type)
&& (target->rtos->type->ps_command)) {
display = target->rtos->type->ps_command(target);
command_print(CMD, "%s", display);
free(display);
return ERROR_OK;
}if ((target->rtos) && (target->rtos->type) && (target->rtos->type->ps_command)) { ... } else {
LOG_INFO("failed");
return ERROR_TARGET_FAILURE;
}else { ... }
}{ ... }
static void binprint(struct command_invocation *cmd, const char *text, const uint8_t *buf, int size)
{
if (text)
command_print_sameline(cmd, "%s", text);
for (int i = 0; i < size; i++)
command_print_sameline(cmd, " %02x", buf[i]);
command_print(cmd, " ");
}{ ... }
#include <flash/nor/imp.h>
COMMAND_HANDLER(handle_report_flash_progress)
{
struct target *target = get_current_target(CMD_CTX);
if (CMD_ARGC == 1)
{
int new_val = 0;
COMMAND_PARSE_ON_OFF(CMD_ARGV[0], new_val);
target->report_flash_progress = new_val;
if (new_val)
{
for (struct flash_bank *bank = flash_bank_list(); bank; bank = bank->next)
{
int r = bank->driver->probe(bank);
if (r != ERROR_OK)
LOG_ERROR("FLASH bank probe failed for %s", bank->name);
command_print(cmd, "flash_bank_summary:0x%x|0x%x|%s", (uint32_t)bank->base, (uint32_t)bank->size, bank->name);
}for (struct flash_bank *bank = flash_bank_list(); bank; bank = bank->next) { ... }
}if (new_val) { ... }
}if (CMD_ARGC == 1) { ... }
command_print(cmd, "FLASH progress reporting is now %s\n", target->report_flash_progress ? "on" : "off");
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(handle_run_until_stop_fast)
{
int timeout = 5000;
if (CMD_ARGC == 1)
{
COMMAND_PARSE_NUMBER(s32, CMD_ARGV[0], timeout);
}if (CMD_ARGC == 1) { ... }
struct target *target = get_current_target(CMD_CTX);
target_resume(target, 1, 0, 1, 0);
if (target_wait_state(target, TARGET_HALTED, timeout) == ERROR_OK)
{
command_print(cmd, "Target successfully stopped");
}if (target_wait_state(target, TARGET_HALTED, timeout) == ERROR_OK) { ... }
else
target_halt(target);
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(handle_wait_for_stop)
{
int timeout = 5000;
if (CMD_ARGC == 1)
{
COMMAND_PARSE_NUMBER(s32, CMD_ARGV[0], timeout);
}if (CMD_ARGC == 1) { ... }
struct target *target = get_current_target(CMD_CTX);
if (target_wait_state(target, TARGET_HALTED, timeout) == ERROR_OK)
command_print(cmd, "Target successfully stopped");
else
command_print(cmd, "Target did not halt within %d msec", timeout);
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(handle_test_mem_access_command)
{
struct target *target = get_current_target(CMD_CTX);
uint32_t test_size;
int retval = ERROR_OK;
if (target->state != TARGET_HALTED) {
command_print(CMD, "Error: [%s] not halted", target_name(target));
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
if (CMD_ARGC != 1)
return ERROR_COMMAND_SYNTAX_ERROR;
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], test_size);
size_t num_bytes = test_size + 4;
struct working_area *wa = NULL;
retval = target_alloc_working_area(target, num_bytes, &wa);
if (retval != ERROR_OK) {
LOG_ERROR("Not enough working area");
return ERROR_FAIL;
}if (retval != ERROR_OK) { ... }
uint8_t *test_pattern = malloc(num_bytes);
for (size_t i = 0; i < num_bytes; i++)
test_pattern[i] = rand();
retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
if (retval != ERROR_OK) {
LOG_ERROR("Test pattern write failed");
goto out;
}if (retval != ERROR_OK) { ... }
for (int host_offset = 0; host_offset <= 1; host_offset++) {
for (int size = 1; size <= 4; size *= 2) {
for (int offset = 0; offset < 4; offset++) {
uint32_t count = test_size / size;
size_t host_bufsiz = (count + 2) * size + host_offset;
uint8_t *read_ref = malloc(host_bufsiz);
uint8_t *read_buf = malloc(host_bufsiz);
for (size_t i = 0; i < host_bufsiz; i++) {
read_ref[i] = rand();
read_buf[i] = read_ref[i];
}for (size_t i = 0; i < host_bufsiz; i++) { ... }
command_print_sameline(CMD,
"Test read %" PRIu32 " x %d @ %d to %saligned buffer: ", count,
size, offset, host_offset ? "un" : "");
struct duration bench;
duration_start(&bench);
retval = target_read_memory(target, wa->address + offset, size, count,
read_buf + size + host_offset);
duration_measure(&bench);
if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
command_print(CMD, "Unsupported alignment");
goto next;
}if (retval == ERROR_TARGET_UNALIGNED_ACCESS) { ... } else if (retval != ERROR_OK) {
command_print(CMD, "Memory read failed");
goto next;
}else if (retval != ERROR_OK) { ... }
memcpy(read_ref + size + host_offset, test_pattern + offset, count * size);
int result = memcmp(read_ref, read_buf, host_bufsiz);
if (result == 0) {
command_print(CMD, "Pass in %fs (%0.3f KiB/s)",
duration_elapsed(&bench),
duration_kbps(&bench, count * size));
}if (result == 0) { ... } else {
command_print(CMD, "Compare failed");
binprint(CMD, "ref:", read_ref, host_bufsiz);
binprint(CMD, "buf:", read_buf, host_bufsiz);
}else { ... }
next:
free(read_ref);
free(read_buf);
}for (int offset = 0; offset < 4; offset++) { ... }
}for (int size = 1; size <= 4; size *= 2) { ... }
}for (int host_offset = 0; host_offset <= 1; host_offset++) { ... }
out:
free(test_pattern);
target_free_working_area(target, wa);
num_bytes = test_size + 4 + 4 + 4;
retval = target_alloc_working_area(target, num_bytes, &wa);
if (retval != ERROR_OK) {
LOG_ERROR("Not enough working area");
return ERROR_FAIL;
}if (retval != ERROR_OK) { ... }
test_pattern = malloc(num_bytes);
for (size_t i = 0; i < num_bytes; i++)
test_pattern[i] = rand();
for (int host_offset = 0; host_offset <= 1; host_offset++) {
for (int size = 1; size <= 4; size *= 2) {
for (int offset = 0; offset < 4; offset++) {
uint32_t count = test_size / size;
size_t host_bufsiz = count * size + host_offset;
uint8_t *read_ref = malloc(num_bytes);
uint8_t *read_buf = malloc(num_bytes);
uint8_t *write_buf = malloc(host_bufsiz);
for (size_t i = 0; i < host_bufsiz; i++)
write_buf[i] = rand();
command_print_sameline(CMD,
"Test write %" PRIu32 " x %d @ %d from %saligned buffer: ", count,
size, offset, host_offset ? "un" : "");
retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
if (retval != ERROR_OK) {
command_print(CMD, "Test pattern write failed");
goto nextw;
}if (retval != ERROR_OK) { ... }
memcpy(read_ref, test_pattern, num_bytes);
memcpy(read_ref + size + offset, write_buf + host_offset, count * size);
struct duration bench;
duration_start(&bench);
retval = target_write_memory(target, wa->address + size + offset, size, count,
write_buf + host_offset);
duration_measure(&bench);
if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
command_print(CMD, "Unsupported alignment");
goto nextw;
}if (retval == ERROR_TARGET_UNALIGNED_ACCESS) { ... } else if (retval != ERROR_OK) {
command_print(CMD, "Memory write failed");
goto nextw;
}else if (retval != ERROR_OK) { ... }
retval = target_read_memory(target, wa->address, 1, num_bytes, read_buf);
if (retval != ERROR_OK) {
command_print(CMD, "Test pattern write failed");
goto nextw;
}if (retval != ERROR_OK) { ... }
int result = memcmp(read_ref, read_buf, num_bytes);
if (result == 0) {
command_print(CMD, "Pass in %fs (%0.3f KiB/s)",
duration_elapsed(&bench),
duration_kbps(&bench, count * size));
}if (result == 0) { ... } else {
command_print(CMD, "Compare failed");
binprint(CMD, "ref:", read_ref, num_bytes);
binprint(CMD, "buf:", read_buf, num_bytes);
}else { ... }
nextw:
free(read_ref);
free(read_buf);
}for (int offset = 0; offset < 4; offset++) { ... }
}for (int size = 1; size <= 4; size *= 2) { ... }
}for (int host_offset = 0; host_offset <= 1; host_offset++) { ... }
free(test_pattern);
target_free_working_area(target, wa);
return retval;
}{ ... }
static const struct command_registration target_exec_command_handlers[] = {
{
.name = "fast_load_image",
.handler = handle_fast_load_image_command,
.mode = COMMAND_ANY,
.help = "Load image into server memory for later use by "
"fast_load; primarily for profiling",
.usage = "filename [address ['bin'|'ihex'|'elf'|'s19' "
"[min_address [max_length]]]]",
...},
{
.name = "fast_load",
.handler = handle_fast_load_command,
.mode = COMMAND_EXEC,
.help = "loads active fast load image to current target "
"- mainly for profiling purposes",
.usage = "",
...},
{
.name = "profile",
.handler = handle_profile_command,
.mode = COMMAND_EXEC,
.usage = "seconds filename [start end]",
.help = "profiling samples the CPU PC",
...},
{
.name = "virt2phys",
.handler = handle_virt2phys_command,
.mode = COMMAND_ANY,
.help = "translate a virtual address into a physical address",
.usage = "virtual_address",
...},
{
.name = "reg",
.handler = handle_reg_command,
.mode = COMMAND_EXEC,
.help = "display (reread from target with \"force\") or set a register; "
"with no arguments, displays all registers and their values",
.usage = "[(register_number|register_name) [(value|'force')]]",
...},
{
.name = "poll",
.handler = handle_poll_command,
.mode = COMMAND_EXEC,
.help = "poll target state; or reconfigure background polling",
.usage = "['on'|'off']",
...},
{
.name = "wait_halt",
.handler = handle_wait_halt_command,
.mode = COMMAND_EXEC,
.help = "wait up to the specified number of milliseconds "
"(default 5000) for a previously requested halt",
.usage = "[milliseconds]",
...},
{
.name = "halt",
.handler = handle_halt_command,
.mode = COMMAND_EXEC,
.help = "request target to halt, then wait up to the specified "
"number of milliseconds (default 5000) for it to complete",
.usage = "[milliseconds]",
...},
{
.name = "resume",
.handler = handle_resume_command,
.mode = COMMAND_EXEC,
.help = "resume target execution from current PC or address",
.usage = "[address]",
...},
{
.name = "reset",
.handler = handle_reset_command,
.mode = COMMAND_EXEC,
.usage = "[run|halt|init]",
.help = "Reset all targets into the specified mode. "
"Default reset mode is run, if not given.",
...},
{
.name = "soft_reset_halt",
.handler = handle_soft_reset_halt_command,
.mode = COMMAND_EXEC,
.usage = "",
.help = "halt the target and do a soft reset",
...},
{
.name = "step",
.handler = handle_step_command,
.mode = COMMAND_EXEC,
.help = "step one instruction from current PC or address",
.usage = "[address]",
...},
{
.name = "mdd",
.handler = handle_md_command,
.mode = COMMAND_EXEC,
.help = "display memory double-words",
.usage = "['phys'] address [count]",
...},
{
.name = "mdw",
.handler = handle_md_command,
.mode = COMMAND_EXEC,
.help = "display memory words",
.usage = "['phys'] address [count]",
...},
{
.name = "mdh",
.handler = handle_md_command,
.mode = COMMAND_EXEC,
.help = "display memory half-words",
.usage = "['phys'] address [count]",
...},
{
.name = "mdb",
.handler = handle_md_command,
.mode = COMMAND_EXEC,
.help = "display memory bytes",
.usage = "['phys'] address [count]",
...},
{
.name = "mwd",
.handler = handle_mw_command,
.mode = COMMAND_EXEC,
.help = "write memory double-word",
.usage = "['phys'] address value [count]",
...},
{
.name = "mww",
.handler = handle_mw_command,
.mode = COMMAND_EXEC,
.help = "write memory word",
.usage = "['phys'] address value [count]",
...},
{
.name = "mwh",
.handler = handle_mw_command,
.mode = COMMAND_EXEC,
.help = "write memory half-word",
.usage = "['phys'] address value [count]",
...},
{
.name = "mwb",
.handler = handle_mw_command,
.mode = COMMAND_EXEC,
.help = "write memory byte",
.usage = "['phys'] address value [count]",
...},
{
.name = "mbatch",
.handler = handle_mbatch_command,
.mode = COMMAND_EXEC,
.help = "execute a batch of memory commands",
.usage = "[unique ID] [list of memory read/write requests]",
...},
{
.name = "bp",
.handler = handle_bp_command,
.mode = COMMAND_EXEC,
.help = "list or set hardware or software breakpoint",
.usage = "[<address> [<asid>] <length> ['hw'|'hw_ctx']]",
...},
{
.name = "rbp",
.handler = handle_rbp_command,
.mode = COMMAND_EXEC,
.help = "remove breakpoint",
.usage = "'all' | address",
...},
{
.name = "wp",
.handler = handle_wp_command,
.mode = COMMAND_EXEC,
.help = "list (no params) or create watchpoints",
.usage = "[address length [('r'|'w'|'a') [value [mask]]]]",
...},
{
.name = "rwp",
.handler = handle_rwp_command,
.mode = COMMAND_EXEC,
.help = "remove watchpoint",
.usage = "'all' | address",
...},
{
.name = "load_image",
.handler = handle_load_image_command,
.mode = COMMAND_EXEC,
.usage = "filename [address ['bin'|'ihex'|'elf'|'s19' "
"[min_address [max_length]]]]",
...},
{
.name = "dump_image",
.handler = handle_dump_image_command,
.mode = COMMAND_EXEC,
.usage = "filename address size",
...},
{
.name = "verify_image_checksum",
.handler = handle_verify_image_checksum_command,
.mode = COMMAND_EXEC,
.usage = "filename [offset [type]]",
...},
{
.name = "verify_image",
.handler = handle_verify_image_command,
.mode = COMMAND_EXEC,
.usage = "filename [offset [type]]",
...},
{
.name = "test_image",
.handler = handle_test_image_command,
.mode = COMMAND_EXEC,
.usage = "filename [offset [type]]",
...},
{
.name = "get_reg",
.mode = COMMAND_EXEC,
.jim_handler = target_jim_get_reg,
.help = "Get register values from the target",
.usage = "list",
...},
{
.name = "set_reg",
.mode = COMMAND_EXEC,
.handler = handle_set_reg_command,
.help = "Set target register values",
.usage = "dict",
...},
{
.name = "read_memory",
.mode = COMMAND_EXEC,
.handler = handle_target_read_memory,
.help = "Read Tcl list of 8/16/32/64 bit numbers from target memory",
.usage = "address width count ['phys']",
...},
{
.name = "write_memory",
.mode = COMMAND_EXEC,
.jim_handler = target_jim_write_memory,
.help = "Write Tcl list of 8/16/32/64 bit numbers to target memory",
.usage = "address width data ['phys']",
...},
{
.name = "debug_reason",
.mode = COMMAND_EXEC,
.handler = handle_target_debug_reason,
.help = "displays the debug reason of this target",
.usage = "",
...},
{
.name = "reset_nag",
.handler = handle_target_reset_nag,
.mode = COMMAND_ANY,
.help = "Nag after each reset about options that could have been "
"enabled to improve performance.",
.usage = "['enable'|'disable']",
...},
{
.name = "ps",
.handler = handle_ps_command,
.mode = COMMAND_EXEC,
.help = "list all tasks",
.usage = "",
...},
{
.name = "test_mem_access",
.handler = handle_test_mem_access_command,
.mode = COMMAND_EXEC,
.help = "Test the target's memory access functions",
.usage = "size",
...},
{
.name = "report_flash_progress",
.handler = handle_report_flash_progress,
.mode = COMMAND_EXEC,
.help = "Enables/disables reporting FLASH programming progress",
.usage = "[on/off]",
...},
{
.name = "run_until_stop_fast",
.handler = handle_run_until_stop_fast,
.mode = COMMAND_EXEC,
.help = "Runs the target until a stop occurs",
.usage = "[timeout]",
...},
{
.name = "wait_for_stop",
.handler = handle_wait_for_stop,
.mode = COMMAND_EXEC,
.help = "Waits for the target to stop",
.usage = "[timeout]",
...},
COMMAND_REGISTRATION_DONE
...};
static int target_register_user_commands(struct command_context *cmd_ctx)
{
int retval = ERROR_OK;
retval = target_request_register_commands(cmd_ctx);
if (retval != ERROR_OK)
return retval;
retval = trace_register_commands(cmd_ctx);
if (retval != ERROR_OK)
return retval;
return register_commands(cmd_ctx, NULL, target_exec_command_handlers);
}{ ... }
const char *target_debug_reason_str(enum target_debug_reason reason)
{
switch (reason) {
case DBG_REASON_DBGRQ:
return "DBGRQ";case DBG_REASON_DBGRQ:
case DBG_REASON_BREAKPOINT:
return "BREAKPOINT";case DBG_REASON_BREAKPOINT:
case DBG_REASON_WATCHPOINT:
return "WATCHPOINT";case DBG_REASON_WATCHPOINT:
case DBG_REASON_WPTANDBKPT:
return "WPTANDBKPT";case DBG_REASON_WPTANDBKPT:
case DBG_REASON_SINGLESTEP:
return "SINGLESTEP";case DBG_REASON_SINGLESTEP:
case DBG_REASON_NOTHALTED:
return "NOTHALTED";case DBG_REASON_NOTHALTED:
case DBG_REASON_EXIT:
return "EXIT";case DBG_REASON_EXIT:
case DBG_REASON_EXC_CATCH:
return "EXC_CATCH";case DBG_REASON_EXC_CATCH:
case DBG_REASON_UNDEFINED:
return "UNDEFINED";case DBG_REASON_UNDEFINED:
default:
return "UNKNOWN!";default
}switch (reason) { ... }
}{ ... }