1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
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
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
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
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
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
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
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
1552
1553
1554
1555
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
1581
1582
1583
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
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
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
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
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
1824
1825
1826
1827
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
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
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
1941
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
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
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
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
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
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
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
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
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
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
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
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
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
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
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
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
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
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
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
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
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
3661
3662
3663
3664
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
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
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
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
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
3824
3825
3826
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
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
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
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
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
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
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
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
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
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
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
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
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
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
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
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
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
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
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
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
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
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
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
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
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
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
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
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
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
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
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
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
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
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
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
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
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
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
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
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
|
/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "anx74xx.h"
#include "battery.h"
#include "comm-host.h"
#include "chipset.h"
#include "compile_time_macros.h"
#include "cros_ec_dev.h"
#include "ec_panicinfo.h"
#include "ec_flash.h"
#include "ec_version.h"
#include "ectool.h"
#include "lightbar.h"
#include "lock/gec_lock.h"
#include "misc_util.h"
#include "panic.h"
#include "ps8xxx.h"
#include "usb_pd.h"
/* Maximum flash size (16 MB, conservative) */
#define MAX_FLASH_SIZE 0x1000000
/* Command line options */
enum {
OPT_DEV = 1000,
OPT_INTERFACE,
OPT_NAME,
OPT_ASCII,
};
static struct option long_opts[] = {
{"dev", 1, 0, OPT_DEV},
{"interface", 1, 0, OPT_INTERFACE},
{"name", 1, 0, OPT_NAME},
{"ascii", 0, 0, OPT_ASCII},
{NULL, 0, 0, 0}
};
#define GEC_LOCK_TIMEOUT_SECS 30 /* 30 secs */
const char help_str[] =
"Commands:\n"
" adcread <channel>\n"
" Read an ADC channel.\n"
" addentropy [reset]\n"
" Add entropy to device secret\n"
" apreset\n"
" Issue AP reset\n"
" autofanctrl <on>\n"
" Turn on automatic fan speed control.\n"
" backlight <enabled>\n"
" Enable/disable LCD backlight\n"
" battery\n"
" Prints battery info\n"
" batterycutoff [at-shutdown]\n"
" Cut off battery output power\n"
" batteryparam\n"
" Read or write board-specific battery parameter\n"
" boardversion\n"
" Prints the board version\n"
" cbi\n"
" Get/Set Cros Board Info\n"
" chargecurrentlimit\n"
" Set the maximum battery charging current\n"
" chargecontrol\n"
" Force the battery to stop charging or discharge\n"
" chargeoverride\n"
" Overrides charge port selection logic\n"
" chargestate\n"
" Handle commands related to charge state v2 (and later)\n"
" chipinfo\n"
" Prints chip info\n"
" cmdversions <cmd>\n"
" Prints supported version mask for a command number\n"
" console\n"
" Prints the last output to the EC debug console\n"
" cec\n"
" Read or write CEC messages and settings\n"
" echash [CMDS]\n"
" Various EC hash commands\n"
" eventclear <mask>\n"
" Clears EC host events flags where mask has bits set\n"
" eventclearb <mask>\n"
" Clears EC host events flags copy B where mask has bits set\n"
" eventget\n"
" Prints raw EC host event flags\n"
" eventgetb\n"
" Prints raw EC host event flags copy B\n"
" eventgetscimask\n"
" Prints SCI mask for EC host events\n"
" eventgetsmimask\n"
" Prints SMI mask for EC host events\n"
" eventgetwakemask\n"
" Prints wake mask for EC host events\n"
" eventsetscimask <mask>\n"
" Sets the SCI mask for EC host events\n"
" eventsetsmimask <mask>\n"
" Sets the SMI mask for EC host events\n"
" eventsetwakemask <mask>\n"
" Sets the wake mask for EC host events\n"
" extpwrlimit\n"
" Set the maximum external power limit\n"
" fanduty <percent>\n"
" Forces the fan PWM to a constant duty cycle\n"
" flasherase <offset> <size>\n"
" Erases EC flash\n"
" flashinfo\n"
" Prints information on the EC flash\n"
" flashspiinfo\n"
" Prints information on EC SPI flash, if present\n"
" flashpd <dev_id> <port> <filename>\n"
" Flash commands over PD\n"
" flashprotect [now] [enable | disable]\n"
" Prints or sets EC flash protection state\n"
" flashread <offset> <size> <outfile>\n"
" Reads from EC flash to a file\n"
" flashwrite <offset> <infile>\n"
" Writes to EC flash from a file\n"
" fpframe\n"
" Retrieve the finger image as a PGM image\n"
" fpinfo\n"
" Prints information about the Fingerprint sensor\n"
" fpmode [capture|deepsleep|fingerdown|fingerup]\n"
" Configure/Read the fingerprint sensor current mode\n"
" fpseed\n"
" Sets the value of the TPM seed.\n"
" fpstats\n"
" Prints timing statisitcs relating to capture and matching\n"
" fptemplate [<infile>|<index 0..2>]\n"
" Add a template if <infile> is provided, else dump it\n"
" forcelidopen <enable>\n"
" Forces the lid switch to open position\n"
" gpioget <GPIO name>\n"
" Get the value of GPIO signal\n"
" gpioset <GPIO name>\n"
" Set the value of GPIO signal\n"
" hangdetect <flags> <event_msec> <reboot_msec> | stop | start\n"
" Configure or start/stop the hang detect timer\n"
" hello\n"
" Checks for basic communication with EC\n"
" hibdelay [sec]\n"
" Set the delay before going into hibernation\n"
" hostsleepstate\n"
" Report host sleep state to the EC\n"
" i2cprotect <port> [status]\n"
" Protect EC's I2C bus\n"
" i2cread\n"
" Read I2C bus\n"
" i2cwrite\n"
" Write I2C bus\n"
" i2cxfer <port> <slave_addr> <read_count> [write bytes...]\n"
" Perform I2C transfer on EC's I2C bus\n"
" infopddev <port>\n"
" Get info about USB type-C accessory attached to port\n"
" inventory\n"
" Return the list of supported features\n"
" kbfactorytest\n"
" Scan out keyboard if any pins are shorted\n"
" kbid\n"
" Get keyboard ID of supported keyboards\n"
" kbinfo\n"
" Dump keyboard matrix dimensions\n"
" kbpress\n"
" Simulate key press\n"
" keyscan <beat_us> <filename>\n"
" Test low-level key scanning\n"
" led <name> <query | auto | off | <color> | <color>=<value>...>\n"
" Set the color of an LED or query brightness range\n"
" lightbar [CMDS]\n"
" Various lightbar control commands\n"
" motionsense [CMDS]\n"
" Various motion sense control commands\n"
" panicinfo\n"
" Prints saved panic info\n"
" pause_in_s5 [on|off]\n"
" Whether or not the AP should pause in S5 on shutdown\n"
" pdcontrol [suspend|resume|reset|disable|on]\n"
" Controls the PD chip\n"
" pdchipinfo <port>\n"
" Get PD chip information\n"
" pdlog\n"
" Prints the PD event log entries\n"
" pdwritelog <type> <port>\n"
" Writes a PD event log of the given <type>\n"
" pdgetmode <port>\n"
" Get All USB-PD alternate SVIDs and modes on <port>\n"
" pdsetmode <port> <svid> <opos>\n"
" Set USB-PD alternate SVID and mode on <port>\n"
" port80flood\n"
" Rapidly write bytes to port 80\n"
" port80read\n"
" Print history of port 80 write\n"
" powerinfo\n"
" Prints power-related information\n"
" protoinfo\n"
" Prints EC host protocol information\n"
" pstoreinfo\n"
" Prints information on the EC host persistent storage\n"
" pstoreread <offset> <size> <outfile>\n"
" Reads from EC host persistent storage to a file\n"
" pstorewrite <offset> <infile>\n"
" Writes to EC host persistent storage from a file\n"
" pwmgetfanrpm [<index> | all]\n"
" Prints current fan RPM\n"
" pwmgetkblight\n"
" Prints current keyboard backlight percent\n"
" pwmgetnumfans\n"
" Prints the number of fans present\n"
" pwmgetduty\n"
" Prints the current 16 bit duty cycle for given PWM\n"
" pwmsetfanrpm <targetrpm>\n"
" Set target fan RPM\n"
" pwmsetkblight <percent>\n"
" Set keyboard backlight in percent\n"
" pwmsetduty\n"
" Set 16 bit duty cycle of given PWM\n"
" readtest <patternoffset> <size>\n"
" Reads a pattern from the EC via LPC\n"
" reboot_ec <RO|RW|cold|hibernate|hibernate-clear-ap-off|disable-jump>"
" [at-shutdown|switch-slot]\n"
" Reboot EC to RO or RW\n"
" rollbackinfo\n"
" Print rollback block information\n"
" rtcget\n"
" Print real-time clock\n"
" rtcgetalarm\n"
" Print # of seconds before real-time clock alarm goes off.\n"
" rtcset <time>\n"
" Set real-time clock\n"
" rtcsetalarm <sec>\n"
" Set real-time clock alarm to go off in <sec> seconds\n"
" rwhashpd <dev_id> <HASH[0] ... <HASH[4]>\n"
" Set entry in PD MCU's device rw_hash table.\n"
" rwsigaction\n"
" Control the behavior of RWSIG task.\n"
" rwsigstatus\n"
" Run RW signature verification and get status.\n"
" sertest\n"
" Serial output test for COM2\n"
" switches\n"
" Prints current EC switch positions\n"
" temps <sensorid>\n"
" Print temperature.\n"
" tempsinfo <sensorid>\n"
" Print temperature sensor info.\n"
" thermalget <platform-specific args>\n"
" Get the threshold temperature values from the thermal engine.\n"
" thermalset <platform-specific args>\n"
" Set the threshold temperature values for the thermal engine.\n"
" tpselftest\n"
" Run touchpad self test.\n"
" tpframeget\n"
" Get touchpad frame data.\n"
" tmp006cal <tmp006_index> [params...]\n"
" Get/set TMP006 calibration\n"
" tmp006raw <tmp006_index>\n"
" Get raw TMP006 data\n"
" uptimeinfo\n"
" Get info about how long the EC has been running and the most\n"
" recent AP resets\n"
" usbchargemode <port> <mode> [<inhibit_charge>]\n"
" Set USB charging mode\n"
" usbmux <mux>\n"
" Set USB mux switch state\n"
" usbpd <port> <auto | "
"[toggle|toggle-off|sink|source] [none|usb|dp|dock] "
"[dr_swap|pr_swap|vconn_swap]>\n"
" Control USB PD/type-C\n"
" usbpdmuxinfo\n"
" Get USB-C SS mux info\n"
" usbpdpower [port]\n"
" Get USB PD power information\n"
" version\n"
" Prints EC version\n"
" waitevent <type> [<timeout>]\n"
" Wait for the MKBP event of type and display it\n"
" wireless <flags> [<mask> [<suspend_flags> <suspend_mask>]]\n"
" Enable/disable WLAN/Bluetooth radio\n"
"";
/* Note: depends on enum system_image_copy_t */
static const char * const image_names[] = {"unknown", "RO", "RW"};
/* Note: depends on enum ec_led_colors */
static const char * const led_color_names[] = {
"red", "green", "blue", "yellow", "white", "amber"};
BUILD_ASSERT(ARRAY_SIZE(led_color_names) == EC_LED_COLOR_COUNT);
/* Note: depends on enum ec_led_id */
static const char * const led_names[] = {
"battery", "power", "adapter", "left", "right", "recovery_hwreinit",
"sysrq debug" };
BUILD_ASSERT(ARRAY_SIZE(led_names) == EC_LED_ID_COUNT);
/* ASCII mode for printing, default off */
static int ascii_mode = 0;
/* Check SBS numerical value range */
int is_battery_range(int val)
{
return (val >= 0 && val <= 65535) ? 1 : 0;
}
int parse_bool(const char *s, int *dest)
{
if (!strcasecmp(s, "off") || !strncasecmp(s, "dis", 3) ||
tolower(*s) == 'f' || tolower(*s) == 'n') {
*dest = 0;
return 1;
} else if (!strcasecmp(s, "on") || !strncasecmp(s, "ena", 3) ||
tolower(*s) == 't' || tolower(*s) == 'y') {
*dest = 1;
return 1;
} else {
return 0;
}
}
void print_help(const char *prog, int print_cmds)
{
printf("Usage: %s [--dev=n] [--interface=dev|i2c|lpc] ", prog);
printf("[--name=cros_ec|cros_fp|cros_pd|cros_scp|cros_sh] [--ascii] ");
printf("<command> [params]\n\n");
if (print_cmds)
puts(help_str);
else
printf("Use '%s help' to print a list of commands.\n", prog);
}
static uint8_t read_mapped_mem8(uint8_t offset)
{
int ret;
uint8_t val;
ret = ec_readmem(offset, sizeof(val), &val);
if (ret <= 0) {
fprintf(stderr, "failure in %s(): %d\n", __func__, ret);
exit(1);
}
return val;
}
static uint16_t read_mapped_mem16(uint8_t offset)
{
int ret;
uint16_t val;
ret = ec_readmem(offset, sizeof(val), &val);
if (ret <= 0) {
fprintf(stderr, "failure in %s(): %d\n", __func__, ret);
exit(1);
}
return val;
}
static uint32_t read_mapped_mem32(uint8_t offset)
{
int ret;
uint32_t val;
ret = ec_readmem(offset, sizeof(val), &val);
if (ret <= 0) {
fprintf(stderr, "failure in %s(): %d\n", __func__, ret);
exit(1);
}
return val;
}
static int read_mapped_string(uint8_t offset, char *buffer, int max_size)
{
int ret;
ret = ec_readmem(offset, max_size, buffer);
if (ret <= 0) {
fprintf(stderr, "failure in %s(): %d\n", __func__, ret);
exit(1);
}
return ret;
}
int cmd_adc_read(int argc, char *argv[])
{
char *e;
struct ec_params_adc_read p;
struct ec_response_adc_read r;
int rv;
if (argc < 2) {
fprintf(stderr, "Usage: %s <adc channel>\n", argv[0]);
return -1;
}
p.adc_channel = (uint8_t)strtoul(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "\"%s\": invalid channel!\n", argv[1]);
return -1;
}
rv = ec_command(EC_CMD_ADC_READ, 0, &p, sizeof(p), &r, sizeof(r));
if (rv > 0) {
printf("%s: %d\n", argv[1], r.adc_value);
return 0;
}
return rv;
}
int cmd_add_entropy(int argc, char *argv[])
{
struct ec_params_rollback_add_entropy p;
int rv;
int tries = 100; /* Wait for 10 seconds at most */
if (argc >= 2 && !strcmp(argv[1], "reset"))
p.action = ADD_ENTROPY_RESET_ASYNC;
else
p.action = ADD_ENTROPY_ASYNC;
rv = ec_command(EC_CMD_ADD_ENTROPY, 0, &p, sizeof(p), NULL, 0);
if (rv != EC_RES_SUCCESS)
goto out;
while (tries--) {
usleep(100000);
p.action = ADD_ENTROPY_GET_RESULT;
rv = ec_command(EC_CMD_ADD_ENTROPY, 0, &p, sizeof(p), NULL, 0);
if (rv == EC_RES_SUCCESS) {
printf("Entropy added successfully\n");
return EC_RES_SUCCESS;
}
/* Abort if EC returns an error other than EC_RES_BUSY. */
if (rv <= -EECRESULT && rv != -EECRESULT-EC_RES_BUSY)
goto out;
}
rv = -EECRESULT-EC_RES_TIMEOUT;
out:
fprintf(stderr, "Failed to add entropy: %d\n", rv);
return rv;
}
int cmd_hello(int argc, char *argv[])
{
struct ec_params_hello p;
struct ec_response_hello r;
int rv;
p.in_data = 0xa0b0c0d0;
rv = ec_command(EC_CMD_HELLO, 0, &p, sizeof(p), &r, sizeof(r));
if (rv < 0)
return rv;
if (r.out_data != 0xa1b2c3d4) {
fprintf(stderr, "Expected response 0x%08x, got 0x%08x\n",
0xa1b2c3d4, r.out_data);
return -1;
}
printf("EC says hello!\n");
return 0;
}
int cmd_hibdelay(int argc, char *argv[])
{
struct ec_params_hibernation_delay p;
struct ec_response_hibernation_delay r;
char *e;
int rv;
if (argc < 2) {
p.seconds = 0; /* Just read the current settings. */
} else {
p.seconds = strtoul(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "invalid number\n");
return -1;
}
}
rv = ec_command(EC_CMD_HIBERNATION_DELAY, 0, &p, sizeof(p),
&r, sizeof(r));
if (rv < 0) {
fprintf(stderr, "err: rv=%d\n", rv);
return -1;
}
printf("Hibernation delay: %u s\n", r.hibernate_delay);
printf("Time G3: %u s\n", r.time_g3);
printf("Time left: %u s\n", r.time_remaining);
return 0;
}
static int get_latest_cmd_version(uint8_t cmd, int *version)
{
struct ec_params_get_cmd_versions p;
struct ec_response_get_cmd_versions r;
int rv;
*version = 0;
/* Figure out the latest version of the given command the EC supports */
p.cmd = cmd;
rv = ec_command(EC_CMD_GET_CMD_VERSIONS, 0, &p, sizeof(p),
&r, sizeof(r));
if (rv < 0) {
if (rv == -EC_RES_INVALID_PARAM)
printf("Command 0x%02x not supported by EC.\n",
EC_CMD_GET_CMD_VERSIONS);
return rv;
}
if (r.version_mask)
*version = __fls(r.version_mask);
return rv;
}
int cmd_hostsleepstate(int argc, char *argv[])
{
struct ec_params_host_sleep_event p;
struct ec_params_host_sleep_event_v1 p1;
struct ec_response_host_sleep_event_v1 r;
void *pp = &p;
size_t psize = sizeof(p), rsize = 0;
char *afterscan;
int rv;
int version = 0, max_version = 0;
uint32_t timeout, transitions;
if (argc < 2) {
fprintf(stderr, "Usage: %s "
"[suspend|wsuspend|resume|freeze|thaw] [timeout]\n",
argv[0]);
return -1;
}
rv = get_latest_cmd_version(EC_CMD_HOST_SLEEP_EVENT, &max_version);
if (rv < 0)
return rv;
if (!strcmp(argv[1], "suspend"))
p.sleep_event = HOST_SLEEP_EVENT_S3_SUSPEND;
else if (!strcmp(argv[1], "wsuspend"))
p.sleep_event = HOST_SLEEP_EVENT_S3_WAKEABLE_SUSPEND;
else if (!strcmp(argv[1], "resume"))
p.sleep_event = HOST_SLEEP_EVENT_S3_RESUME;
else if (!strcmp(argv[1], "freeze")) {
p.sleep_event = HOST_SLEEP_EVENT_S0IX_SUSPEND;
if (max_version >= 1) {
p1.sleep_event = p.sleep_event;
p1.reserved = 0;
p1.suspend_params.sleep_timeout_ms =
EC_HOST_SLEEP_TIMEOUT_DEFAULT;
if (argc > 2) {
p1.suspend_params.sleep_timeout_ms =
strtoul(argv[2], &afterscan, 0);
if ((*afterscan != '\0') ||
(afterscan == argv[2])) {
fprintf(stderr,
"Invalid value: %s\n",
argv[2]);
return -1;
}
}
pp = &p1;
psize = sizeof(p1);
version = 1;
}
} else if (!strcmp(argv[1], "thaw")) {
p.sleep_event = HOST_SLEEP_EVENT_S0IX_RESUME;
if (max_version >= 1) {
version = 1;
rsize = sizeof(r);
}
} else {
fprintf(stderr, "Unknown command: %s\n", argv[1]);
return -1;
}
rv = ec_command(EC_CMD_HOST_SLEEP_EVENT, version, pp, psize, &r, rsize);
if (rv < 0) {
fprintf(stderr, "EC host sleep command failed: %d\n", rv);
return rv;
}
if (rsize) {
timeout = r.resume_response.sleep_transitions &
EC_HOST_RESUME_SLEEP_TIMEOUT;
transitions = r.resume_response.sleep_transitions &
EC_HOST_RESUME_SLEEP_TRANSITIONS_MASK;
printf("%s%d sleep line transitions.\n",
timeout ? "Timeout: " : "",
transitions);
}
return 0;
}
int cmd_test(int argc, char *argv[])
{
struct ec_params_test_protocol p = {
.buf = "0123456789abcdef0123456789ABCDEF"
};
struct ec_response_test_protocol r;
int rv, version = 0;
char *e;
if (argc < 3) {
fprintf(stderr, "Usage: %s result length [version]\n",
argv[0]);
return -1;
}
p.ec_result = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "invalid param (result)\n");
return -1;
}
p.ret_len = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "invalid param (length)\n");
return -1;
}
if (argc > 3) {
version = strtol(argv[3], &e, 0);
if (e && *e) {
fprintf(stderr, "invalid param (version)\n");
return -1;
}
}
rv = ec_command(EC_CMD_TEST_PROTOCOL, version,
&p, sizeof(p), &r, sizeof(r));
printf("rv = %d\n", rv);
return rv;
}
int cmd_s5(int argc, char *argv[])
{
struct ec_params_get_set_value p;
struct ec_params_get_set_value r;
int rv, param;
p.flags = 0;
if (argc > 1) {
p.flags |= EC_GSV_SET;
if (!parse_bool(argv[1], ¶m)) {
fprintf(stderr, "invalid arg \"%s\"\n", argv[1]);
return -1;
}
p.value = param;
}
rv = ec_command(EC_CMD_GSV_PAUSE_IN_S5, 0,
&p, sizeof(p), &r, sizeof(r));
if (rv > 0)
printf("%s\n", r.value ? "on" : "off");
return rv < 0;
}
static const char * const ec_feature_names[] = {
[EC_FEATURE_LIMITED] = "Limited image, load RW for more",
[EC_FEATURE_FLASH] = "Flash",
[EC_FEATURE_PWM_FAN] = "Direct Fan power management",
[EC_FEATURE_PWM_KEYB] = "Keyboard backlight",
[EC_FEATURE_LIGHTBAR] = "Lightbar",
[EC_FEATURE_LED] = "LED",
[EC_FEATURE_MOTION_SENSE] = "Motion Sensors",
[EC_FEATURE_KEYB] = "Keyboard",
[EC_FEATURE_PSTORE] = "Host Permanent Storage",
[EC_FEATURE_PORT80] = "BIOS Port 80h access",
[EC_FEATURE_THERMAL] = "Thermal management",
[EC_FEATURE_BKLIGHT_SWITCH] = "Switch backlight on/off",
[EC_FEATURE_WIFI_SWITCH] = "Switch wifi on/off",
[EC_FEATURE_HOST_EVENTS] = "Host event",
[EC_FEATURE_GPIO] = "GPIO",
[EC_FEATURE_I2C] = "I2C master",
[EC_FEATURE_CHARGER] = "Charger",
[EC_FEATURE_BATTERY] = "Simple Battery",
[EC_FEATURE_SMART_BATTERY] = "Smart Battery",
[EC_FEATURE_HANG_DETECT] = "Host hang detection",
[EC_FEATURE_PMU] = "Power Management",
[EC_FEATURE_SUB_MCU] = "Control downstream MCU",
[EC_FEATURE_USB_PD] = "USB Cros Power Delivery",
[EC_FEATURE_USB_MUX] = "USB Multiplexer",
[EC_FEATURE_MOTION_SENSE_FIFO] = "FIFO for Motion Sensors events",
[EC_FEATURE_VSTORE] = "Temporary secure vstore",
[EC_FEATURE_USBC_SS_MUX_VIRTUAL] = "Host-controlled USB-C SS mux",
[EC_FEATURE_RTC] = "Real-time clock",
[EC_FEATURE_FINGERPRINT] = "Fingerprint",
[EC_FEATURE_TOUCHPAD] = "Touchpad",
[EC_FEATURE_RWSIG] = "RWSIG task",
[EC_FEATURE_DEVICE_EVENT] = "Device events reporting",
[EC_FEATURE_UNIFIED_WAKE_MASKS] = "Unified wake masks for LPC/eSPI",
[EC_FEATURE_HOST_EVENT64] = "64-bit host events",
[EC_FEATURE_EXEC_IN_RAM] = "Execute code in RAM",
[EC_FEATURE_CEC] = "Consumer Electronics Control",
[EC_FEATURE_MOTION_SENSE_TIGHT_TIMESTAMPS] =
"Tight timestamp for sensors events",
[EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS] =
"Refined tablet mode hysteresis",
[EC_FEATURE_AUDIO_CODEC] = "Audio Codec",
[EC_FEATURE_ISH] = "Intel Integrated Sensor Hub",
};
int cmd_inventory(int argc, char *argv[])
{
struct ec_response_get_features r;
int rv, i, j, idx;
rv = ec_command(EC_CMD_GET_FEATURES, 0, NULL, 0, &r, sizeof(r));
if (rv < 0)
return rv;
printf("EC supported features:\n");
for (i = 0, idx = 0; i < 2; i++) {
for (j = 0; j < 32; j++, idx++) {
if (r.flags[i] & BIT(j)) {
if (idx >= ARRAY_SIZE(ec_feature_names) ||
!ec_feature_names[idx] ||
strlen(ec_feature_names[idx]) == 0)
printf("%-4d: Unknown feature\n", idx);
else
printf("%-4d: %s support\n",
idx, ec_feature_names[idx]);
}
}
}
return 0;
}
int cmd_cmdversions(int argc, char *argv[])
{
struct ec_params_get_cmd_versions p;
struct ec_response_get_cmd_versions r;
char *e;
int cmd;
int rv;
if (argc < 2) {
fprintf(stderr, "Usage: %s <cmd>\n", argv[0]);
return -1;
}
cmd = strtol(argv[1], &e, 0);
if ((e && *e) || cmd < 0 || cmd > 0xff) {
fprintf(stderr, "Bad command number.\n");
return -1;
}
p.cmd = cmd;
rv = ec_command(EC_CMD_GET_CMD_VERSIONS, 0, &p, sizeof(p),
&r, sizeof(r));
if (rv < 0) {
if (rv == -EC_RES_INVALID_PARAM)
printf("Command 0x%02x not supported by EC.\n", cmd);
return rv;
}
printf("Command 0x%02x supports version mask 0x%08x\n",
cmd, r.version_mask);
return 0;
}
/*
* Convert a reset cause ID to human-readable string, providing total coverage
* of the 'cause' space. The returned string points to static storage and must
* not be free()ed.
*/
static const char *reset_cause_to_str(uint16_t cause)
{
static const char * const reset_causes[] = {
"(reset unknown)",
"reset: board custom",
"reset: ap hang detected",
"reset: console command",
"reset: host command",
"reset: keyboard sysreset",
"reset: keyboard warm reboot",
"reset: debug warm reboot",
"reset: at AP's request",
"reset: during EC initialization",
"reset: AP watchdog",
};
BUILD_ASSERT(ARRAY_SIZE(reset_causes) == CHIPSET_RESET_COUNT);
static const char * const shutdown_causes[] = {
"shutdown: power failure",
"shutdown: during EC initialization",
"shutdown: board custom",
"shutdown: battery voltage startup inhibit",
"shutdown: power wait asserted",
"shutdown: critical battery",
"shutdown: by console command",
"shutdown: entering G3",
"shutdown: thermal",
"shutdown: power button",
};
BUILD_ASSERT(ARRAY_SIZE(shutdown_causes) ==
CHIPSET_SHUTDOWN_COUNT - CHIPSET_SHUTDOWN_BEGIN);
if (cause < CHIPSET_RESET_COUNT)
return reset_causes[cause];
if (cause < CHIPSET_SHUTDOWN_BEGIN)
return "(reset unknown)";
if (cause < CHIPSET_SHUTDOWN_COUNT)
return shutdown_causes[cause - CHIPSET_SHUTDOWN_BEGIN];
return "(shutdown unknown)";
}
int cmd_uptimeinfo(int argc, char *argv[])
{
static const char * const reset_flag_strings[] = {
"other",
"reset-pin",
"brownout",
"power-on",
"watchdog",
"soft",
"hibernate",
"rtc-alarm",
"wake-pin",
"low-battery",
"sysjump",
"hard",
"ap-off",
"preserved",
"usb-resume",
"rdd",
"rbox",
"security",
"ap-watchdog"
};
struct ec_response_uptime_info r;
int rv;
int i;
int flag_count;
uint32_t flag;
if (argc != 1) {
fprintf(stderr, "uptimeinfo takes no arguments");
return -1;
}
rv = ec_command(EC_CMD_GET_UPTIME_INFO, 0, NULL, 0, &r, sizeof(r));
if (rv < 0) {
fprintf(stderr, "ERROR: EC_CMD_GET_UPTIME_INFO failed; %d\n",
rv);
return rv;
}
printf("EC uptime: %d.%03d seconds\n",
r.time_since_ec_boot_ms / 1000,
r.time_since_ec_boot_ms % 1000);
printf("AP resets since EC boot: %d\n", r.ap_resets_since_ec_boot);
printf("Most recent AP reset causes:\n");
for (i = 0; i != ARRAY_SIZE(r.recent_ap_reset); ++i) {
if (r.recent_ap_reset[i].reset_time_ms == 0)
continue;
printf("\t%d.%03d: %s\n",
r.recent_ap_reset[i].reset_time_ms / 1000,
r.recent_ap_reset[i].reset_time_ms % 1000,
reset_cause_to_str(r.recent_ap_reset[i].reset_cause));
}
printf("EC reset flags at last EC boot: ");
flag_count = 0;
for (flag = 0; flag != ARRAY_SIZE(reset_flag_strings); ++flag) {
if ((r.ec_reset_flags & BIT(flag)) != 0) {
if (flag_count)
printf(" | ");
printf(reset_flag_strings[flag]);
flag_count++;
}
}
printf("\n");
return 0;
}
int cmd_version(int argc, char *argv[])
{
struct ec_response_get_version r;
char *build_string = (char *)ec_inbuf;
int rv;
rv = ec_command(EC_CMD_GET_VERSION, 0, NULL, 0, &r, sizeof(r));
if (rv < 0) {
fprintf(stderr, "ERROR: EC_CMD_GET_VERSION failed: %d\n", rv);
goto exit;
}
rv = ec_command(EC_CMD_GET_BUILD_INFO, 0,
NULL, 0, ec_inbuf, ec_max_insize);
if (rv < 0) {
fprintf(stderr, "ERROR: EC_CMD_GET_BUILD_INFO failed: %d\n",
rv);
goto exit;
}
rv = 0;
/* Ensure versions are null-terminated before we print them */
r.version_string_ro[sizeof(r.version_string_ro) - 1] = '\0';
r.version_string_rw[sizeof(r.version_string_rw) - 1] = '\0';
build_string[ec_max_insize - 1] = '\0';
/* Print versions */
printf("RO version: %s\n", r.version_string_ro);
printf("RW version: %s\n", r.version_string_rw);
printf("Firmware copy: %s\n",
(r.current_image < ARRAY_SIZE(image_names) ?
image_names[r.current_image] : "?"));
printf("Build info: %s\n", build_string);
exit:
printf("Tool version: %s %s %s\n", CROS_ECTOOL_VERSION, DATE, BUILDER);
return rv;
}
int cmd_read_test(int argc, char *argv[])
{
struct ec_params_read_test p;
struct ec_response_read_test r;
int offset, size;
int errors = 0;
int rv;
int i;
char *e;
char *buf;
uint32_t *b;
if (argc < 3) {
fprintf(stderr, "Usage: %s <pattern_offset> <size>\n", argv[0]);
return -1;
}
offset = strtol(argv[1], &e, 0);
size = strtol(argv[2], &e, 0);
if ((e && *e) || size <= 0 || size > MAX_FLASH_SIZE) {
fprintf(stderr, "Bad size.\n");
return -1;
}
printf("Reading %d bytes with pattern offset 0x%x...\n", size, offset);
buf = (char *)malloc(size);
if (!buf) {
fprintf(stderr, "Unable to allocate buffer.\n");
return -1;
}
/* Read data in chunks */
for (i = 0; i < size; i += sizeof(r.data)) {
p.offset = offset + i / sizeof(uint32_t);
p.size = MIN(size - i, sizeof(r.data));
rv = ec_command(EC_CMD_READ_TEST, 0, &p, sizeof(p),
&r, sizeof(r));
if (rv < 0) {
fprintf(stderr, "Read error at offset %d\n", i);
free(buf);
return rv;
}
memcpy(buf + i, r.data, p.size);
}
/* Check data */
for (i = 0, b = (uint32_t *)buf; i < size / 4; i++, b++) {
if (*b != i + offset) {
printf("Mismatch at byte offset 0x%x: "
"expected 0x%08x, got 0x%08x\n",
(int)(i * sizeof(uint32_t)), i + offset, *b);
errors++;
}
}
free(buf);
if (errors) {
printf("Found %d errors\n", errors);
return -1;
}
printf("done.\n");
return 0;
}
int cmd_reboot_ec(int argc, char *argv[])
{
struct ec_params_reboot_ec p;
int rv, i;
if (argc < 2) {
/*
* No params specified so tell the EC to reboot immediately.
* That reboots the AP as well, so unlikely we'll be around
* to see a return code from this...
*/
rv = ec_command(EC_CMD_REBOOT, 0, NULL, 0, NULL, 0);
return (rv < 0 ? rv : 0);
}
/* Parse command */
if (!strcmp(argv[1], "cancel"))
p.cmd = EC_REBOOT_CANCEL;
else if (!strcmp(argv[1], "RO"))
p.cmd = EC_REBOOT_JUMP_RO;
else if (!strcmp(argv[1], "RW"))
p.cmd = EC_REBOOT_JUMP_RW;
else if (!strcmp(argv[1], "cold"))
p.cmd = EC_REBOOT_COLD;
else if (!strcmp(argv[1], "disable-jump"))
p.cmd = EC_REBOOT_DISABLE_JUMP;
else if (!strcmp(argv[1], "hibernate"))
p.cmd = EC_REBOOT_HIBERNATE;
else if (!strcmp(argv[1], "hibernate-clear-ap-off"))
p.cmd = EC_REBOOT_HIBERNATE_CLEAR_AP_OFF;
else {
fprintf(stderr, "Unknown command: %s\n", argv[1]);
return -1;
}
/* Parse flags, if any */
p.flags = 0;
for (i = 2; i < argc; i++) {
if (!strcmp(argv[i], "at-shutdown")) {
p.flags |= EC_REBOOT_FLAG_ON_AP_SHUTDOWN;
} else if (!strcmp(argv[i], "switch-slot")) {
p.flags |= EC_REBOOT_FLAG_SWITCH_RW_SLOT;
} else {
fprintf(stderr, "Unknown flag: %s\n", argv[i]);
return -1;
}
}
rv = ec_command(EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0);
return (rv < 0 ? rv : 0);
}
int cmd_flash_info(int argc, char *argv[])
{
struct ec_response_flash_info_1 r;
int cmdver = 1;
int rsize = sizeof(r);
int rv;
memset(&r, 0, sizeof(r));
if (!ec_cmd_version_supported(EC_CMD_FLASH_INFO, cmdver)) {
/* Fall back to version 0 command */
cmdver = 0;
rsize = sizeof(struct ec_response_flash_info);
}
rv = ec_command(EC_CMD_FLASH_INFO, cmdver, NULL, 0, &r, rsize);
if (rv < 0)
return rv;
printf("FlashSize %d\nWriteSize %d\nEraseSize %d\nProtectSize %d\n",
r.flash_size, r.write_block_size, r.erase_block_size,
r.protect_block_size);
if (cmdver >= 1) {
/* Fields added in ver.1 available */
printf("WriteIdealSize %d\nFlags 0x%x\n",
r.write_ideal_size, r.flags);
}
return 0;
}
int cmd_flash_spi_info(int argc, char *argv[])
{
struct ec_response_flash_spi_info r;
int rv;
memset(&r, 0, sizeof(r));
/* Print SPI flash info if available */
if (!ec_cmd_version_supported(EC_CMD_FLASH_SPI_INFO, 0)) {
printf("EC has no info (does not use SPI flash?)\n");
return -1;
}
rv = ec_command(EC_CMD_FLASH_SPI_INFO, 0, NULL, 0, &r, sizeof(r));
if (rv < 0)
return rv;
printf("JEDECManufacturerID 0x%02x\n", r.jedec[0]);
printf("JEDECDeviceID 0x%02x 0x%02x\n", r.jedec[1], r.jedec[2]);
printf("JEDECCapacity %d\n", 1 << r.jedec[2]);
printf("ManufacturerID 0x%02x\n", r.mfr_dev_id[0]);
printf("DeviceID 0x%02x\n", r.mfr_dev_id[1]);
printf("StatusRegister1 0x%02x\n", r.sr1);
printf("StatusRegister2 0x%02x\n", r.sr2);
return 0;
}
int cmd_flash_read(int argc, char *argv[])
{
int offset, size;
int rv;
char *e;
char *buf;
if (argc < 4) {
fprintf(stderr,
"Usage: %s <offset> <size> <filename>\n", argv[0]);
return -1;
}
offset = strtol(argv[1], &e, 0);
if ((e && *e) || offset < 0 || offset > MAX_FLASH_SIZE) {
fprintf(stderr, "Bad offset.\n");
return -1;
}
size = strtol(argv[2], &e, 0);
if ((e && *e) || size <= 0 || size > MAX_FLASH_SIZE) {
fprintf(stderr, "Bad size.\n");
return -1;
}
printf("Reading %d bytes at offset %d...\n", size, offset);
buf = (char *)malloc(size);
if (!buf) {
fprintf(stderr, "Unable to allocate buffer.\n");
return -1;
}
/* Read data in chunks */
rv = ec_flash_read(buf, offset, size);
if (rv < 0) {
free(buf);
return rv;
}
rv = write_file(argv[3], buf, size);
free(buf);
if (rv)
return rv;
printf("done.\n");
return 0;
}
int cmd_flash_write(int argc, char *argv[])
{
int offset, size;
int rv;
char *e;
char *buf;
if (argc < 3) {
fprintf(stderr, "Usage: %s <offset> <filename>\n", argv[0]);
return -1;
}
offset = strtol(argv[1], &e, 0);
if ((e && *e) || offset < 0 || offset > MAX_FLASH_SIZE) {
fprintf(stderr, "Bad offset.\n");
return -1;
}
/* Read the input file */
buf = read_file(argv[2], &size);
if (!buf)
return -1;
printf("Writing to offset %d...\n", offset);
/* Write data in chunks */
rv = ec_flash_write(buf, offset, size);
free(buf);
if (rv < 0)
return rv;
printf("done.\n");
return 0;
}
int cmd_flash_erase(int argc, char *argv[])
{
int offset, size;
char *e;
int rv;
if (argc < 3) {
fprintf(stderr, "Usage: %s <offset> <size>\n", argv[0]);
return -1;
}
offset = strtol(argv[1], &e, 0);
if ((e && *e) || offset < 0 || offset > MAX_FLASH_SIZE) {
fprintf(stderr, "Bad offset.\n");
return -1;
}
size = strtol(argv[2], &e, 0);
if ((e && *e) || size <= 0 || size > MAX_FLASH_SIZE) {
fprintf(stderr, "Bad size.\n");
return -1;
}
printf("Erasing %d bytes at offset %d...\n", size, offset);
rv = ec_flash_erase(offset, size);
if (rv < 0)
return rv;
printf("done.\n");
return 0;
}
static void print_flash_protect_flags(const char *desc, uint32_t flags)
{
printf("%s 0x%08x", desc, flags);
if (flags & EC_FLASH_PROTECT_GPIO_ASSERTED)
printf(" wp_gpio_asserted");
if (flags & EC_FLASH_PROTECT_RO_AT_BOOT)
printf(" ro_at_boot");
if (flags & EC_FLASH_PROTECT_RW_AT_BOOT)
printf(" rw_at_boot");
if (flags & EC_FLASH_PROTECT_ROLLBACK_AT_BOOT)
printf(" rollback_at_boot");
if (flags & EC_FLASH_PROTECT_ALL_AT_BOOT)
printf(" all_at_boot");
if (flags & EC_FLASH_PROTECT_RO_NOW)
printf(" ro_now");
if (flags & EC_FLASH_PROTECT_RW_NOW)
printf(" rw_now");
if (flags & EC_FLASH_PROTECT_ROLLBACK_NOW)
printf(" rollback_now");
if (flags & EC_FLASH_PROTECT_ALL_NOW)
printf(" all_now");
if (flags & EC_FLASH_PROTECT_ERROR_STUCK)
printf(" STUCK");
if (flags & EC_FLASH_PROTECT_ERROR_INCONSISTENT)
printf(" INCONSISTENT");
printf("\n");
}
int cmd_flash_protect(int argc, char *argv[])
{
struct ec_params_flash_protect p;
struct ec_response_flash_protect r;
int rv, i;
/*
* Set up requested flags. If no flags were specified, p.mask will
* be 0 and nothing will change.
*/
p.mask = p.flags = 0;
for (i = 1; i < argc; i++) {
if (!strcasecmp(argv[i], "now")) {
p.mask |= EC_FLASH_PROTECT_ALL_NOW;
p.flags |= EC_FLASH_PROTECT_ALL_NOW;
} else if (!strcasecmp(argv[i], "enable")) {
p.mask |= EC_FLASH_PROTECT_RO_AT_BOOT;
p.flags |= EC_FLASH_PROTECT_RO_AT_BOOT;
} else if (!strcasecmp(argv[i], "disable"))
p.mask |= EC_FLASH_PROTECT_RO_AT_BOOT;
}
rv = ec_command(EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
&p, sizeof(p), &r, sizeof(r));
if (rv < 0)
return rv;
if (rv < sizeof(r)) {
fprintf(stderr, "Too little data returned.\n");
return -1;
}
/* Print returned flags */
print_flash_protect_flags("Flash protect flags:", r.flags);
print_flash_protect_flags("Valid flags: ", r.valid_flags);
print_flash_protect_flags("Writable flags: ", r.writable_flags);
/* Check if we got all the flags we asked for */
if ((r.flags & p.mask) != (p.flags & p.mask)) {
fprintf(stderr, "Unable to set requested flags "
"(wanted mask 0x%08x flags 0x%08x)\n",
p.mask, p.flags);
if (p.mask & ~r.writable_flags)
fprintf(stderr, "Which is expected, because writable "
"mask is 0x%08x.\n", r.writable_flags);
return -1;
}
return 0;
}
int cmd_rw_hash_pd(int argc, char *argv[])
{
struct ec_params_usb_pd_rw_hash_entry *p =
(struct ec_params_usb_pd_rw_hash_entry *)ec_outbuf;
int i, rv;
char *e;
uint32_t val;
uint8_t *rwp;
if (argc < 7) {
fprintf(stderr, "Usage: %s <dev_id> <HASH[0]> ... <HASH[4]>\n",
argv[0]);
return -1;
}
p->dev_id = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad device ID\n");
return -1;
}
rwp = p->dev_rw_hash;
for (i = 2; i < 7; i++) {
val = strtol(argv[i], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad RW hash\n");
return -1;
}
rwp[0] = (uint8_t) (val >> 0) & 0xff;
rwp[1] = (uint8_t) (val >> 8) & 0xff;
rwp[2] = (uint8_t) (val >> 16) & 0xff;
rwp[3] = (uint8_t) (val >> 24) & 0xff;
rwp += 4;
}
rv = ec_command(EC_CMD_USB_PD_RW_HASH_ENTRY, 0, p, sizeof(*p), NULL, 0);
return rv;
}
int cmd_rwsig_status(int argc, char *argv[])
{
int rv;
struct ec_response_rwsig_check_status resp;
rv = ec_command(EC_CMD_RWSIG_CHECK_STATUS, 0, NULL, 0,
&resp, sizeof(resp));
if (rv < 0)
return rv;
printf("RW signature check: %s\n", resp.status ? "OK" : "FAILED");
return 0;
}
int cmd_rwsig_action(int argc, char *argv[])
{
struct ec_params_rwsig_action req;
if (argc < 2) {
fprintf(stderr, "Usage: %s abort | continue\n", argv[0]);
return -1;
}
if (!strcasecmp(argv[1], "abort"))
req.action = RWSIG_ACTION_ABORT;
else if (!strcasecmp(argv[1], "continue"))
req.action = RWSIG_ACTION_CONTINUE;
else
return -1;
return ec_command(EC_CMD_RWSIG_ACTION, 0, &req, sizeof(req), NULL, 0);
}
int cmd_rollback_info(int argc, char *argv[])
{
struct ec_response_rollback_info r;
int rv;
rv = ec_command(EC_CMD_ROLLBACK_INFO, 0, NULL, 0, &r, sizeof(r));
if (rv < 0) {
fprintf(stderr, "ERROR: EC_CMD_ROLLBACK_INFO failed: %d\n", rv);
return rv;
}
/* Print versions */
printf("Rollback block id: %d\n", r.id);
printf("Rollback min version: %d\n", r.rollback_min_version);
printf("RW rollback version: %d\n", r.rw_rollback_version);
return 0;
}
int cmd_apreset(int argc, char *argv[])
{
return ec_command(EC_CMD_AP_RESET, 0, NULL, 0, NULL, 0);
}
#define FP_FRAME_INDEX_SIMPLE_IMAGE -1
/*
* Download a frame buffer from the FPMCU.
*
* Might be either the finger image or a finger template depending on 'index'.
*
* @param info a pointer to store the struct ec_response_fp_info retrieved by
* this command.
* @param index the specific frame to retrieve, might be:
* -1 (aka FP_FRAME_INDEX_SIMPLE_IMAGE) for the a single grayscale image.
* 0 (aka FP_FRAME_INDEX_RAW_IMAGE) for the full vendor raw finger image.
* 1..n for a finger template.
*
* @returns a pointer to the buffer allocated to contain the frame or NULL
* if case of error. The caller must call free() once it no longer needs the
* buffer.
*/
static void *fp_download_frame(struct ec_response_fp_info *info, int index)
{
struct ec_params_fp_frame p;
int rv = 0;
size_t stride, size;
void *buffer;
uint8_t *ptr;
int cmdver = ec_cmd_version_supported(EC_CMD_FP_INFO, 1) ? 1 : 0;
int rsize = cmdver == 1 ? sizeof(*info)
: sizeof(struct ec_response_fp_info_v0);
/* templates not supported in command v0 */
if (index > 0 && cmdver == 0)
return NULL;
rv = ec_command(EC_CMD_FP_INFO, cmdver, NULL, 0, info, rsize);
if (rv < 0)
return NULL;
if (index == FP_FRAME_INDEX_SIMPLE_IMAGE) {
size = (size_t)info->width * info->bpp/8 * info->height;
index = FP_FRAME_INDEX_RAW_IMAGE;
} else if (index == FP_FRAME_INDEX_RAW_IMAGE) {
size = info->frame_size;
} else {
size = info->template_size;
}
buffer = malloc(size);
if (!buffer) {
fprintf(stderr, "Cannot allocate memory for the image\n");
return NULL;
}
ptr = buffer;
p.offset = index << FP_FRAME_INDEX_SHIFT;
while (size) {
stride = MIN(ec_max_insize, size);
p.size = stride;
rv = ec_command(EC_CMD_FP_FRAME, 0, &p, sizeof(p),
ptr, stride);
if (rv < 0) {
free(buffer);
return NULL;
}
p.offset += stride;
size -= stride;
ptr += stride;
}
return buffer;
}
int cmd_fp_mode(int argc, char *argv[])
{
struct ec_params_fp_mode p;
struct ec_response_fp_mode r;
uint32_t mode = 0;
uint32_t capture_type = FP_CAPTURE_SIMPLE_IMAGE;
int i, rv;
if (argc == 1)
mode = FP_MODE_DONT_CHANGE;
for (i = 1; i < argc; i++) {
/* modes */
if (!strncmp(argv[i], "deepsleep", 9))
mode |= FP_MODE_DEEPSLEEP;
else if (!strncmp(argv[i], "fingerdown", 10))
mode |= FP_MODE_FINGER_DOWN;
else if (!strncmp(argv[i], "fingerup", 8))
mode |= FP_MODE_FINGER_UP;
else if (!strncmp(argv[i], "enroll", 6))
mode |= FP_MODE_ENROLL_IMAGE | FP_MODE_ENROLL_SESSION;
else if (!strncmp(argv[i], "match", 5))
mode |= FP_MODE_MATCH;
else if (!strncmp(argv[i], "reset_sensor", 12))
mode = FP_MODE_RESET_SENSOR;
else if (!strncmp(argv[i], "reset", 5))
mode = 0;
else if (!strncmp(argv[i], "capture", 7))
mode |= FP_MODE_CAPTURE;
/* capture types */
else if (!strncmp(argv[i], "vendor", 6))
capture_type = FP_CAPTURE_VENDOR_FORMAT;
else if (!strncmp(argv[i], "pattern0", 8))
capture_type = FP_CAPTURE_PATTERN0;
else if (!strncmp(argv[i], "pattern1", 8))
capture_type = FP_CAPTURE_PATTERN1;
else if (!strncmp(argv[i], "qual", 4))
capture_type = FP_CAPTURE_QUALITY_TEST;
else if (!strncmp(argv[i], "test_reset", 10))
capture_type = FP_CAPTURE_RESET_TEST;
}
if (mode & FP_MODE_CAPTURE)
mode |= capture_type << FP_MODE_CAPTURE_TYPE_SHIFT;
p.mode = mode;
rv = ec_command(EC_CMD_FP_MODE, 0, &p, sizeof(p), &r, sizeof(r));
if (rv < 0)
return rv;
printf("FP mode: (0x%x) ", r.mode);
if (r.mode & FP_MODE_DEEPSLEEP)
printf("deepsleep ");
if (r.mode & FP_MODE_FINGER_DOWN)
printf("finger-down ");
if (r.mode & FP_MODE_FINGER_UP)
printf("finger-up ");
if (r.mode & FP_MODE_ENROLL_SESSION)
printf("enroll%s ",
r.mode & FP_MODE_ENROLL_IMAGE ? "+image" : "");
if (r.mode & FP_MODE_MATCH)
printf("match ");
if (r.mode & FP_MODE_CAPTURE)
printf("capture ");
printf("\n");
return 0;
}
int cmd_fp_seed(int argc, char *argv[])
{
struct ec_params_fp_seed p;
const char *seed = argv[1];
int rv;
if (argc == 1) {
printf("Missing seed argument.\n");
return 1;
}
if (strlen(seed) != FP_CONTEXT_TPM_BYTES) {
printf("Invalid seed '%s' is %zd bytes long instead of %d.\n",
seed, strlen(seed), FP_CONTEXT_TPM_BYTES);
return 1;
}
printf("Setting seed '%s'\n", seed);
p.struct_version = FP_TEMPLATE_FORMAT_VERSION;
memcpy(p.seed, seed, FP_CONTEXT_TPM_BYTES);
rv = ec_command(EC_CMD_FP_SEED, 0, &p, sizeof(p), NULL, 0);
return rv;
}
int cmd_fp_stats(int argc, char *argv[])
{
struct ec_response_fp_stats r;
int rv;
unsigned long long ts;
rv = ec_command(EC_CMD_FP_STATS, 0, NULL, 0, &r, sizeof(r));
if (rv < 0)
return rv;
ts = (uint64_t)r.overall_t0.hi << 32 | r.overall_t0.lo;
printf("FP stats (t0=%llu us):\n", ts);
printf("Last capture time: ");
if (r.timestamps_invalid & FPSTATS_CAPTURE_INV)
printf("Invalid\n");
else
printf("%d us\n", r.capture_time_us);
printf("Last matching time: ");
if (r.timestamps_invalid & FPSTATS_MATCHING_INV)
printf("Invalid\n");
else
printf("%d us (finger: %d)\n", r.matching_time_us,
r.template_matched);
printf("Last overall time: ");
if (r.timestamps_invalid)
printf("Invalid\n");
else
printf("%d us\n", r.overall_time_us);
return 0;
}
int cmd_fp_info(int argc, char *argv[])
{
struct ec_response_fp_info r;
int rv;
int cmdver = ec_cmd_version_supported(EC_CMD_FP_INFO, 1) ? 1 : 0;
int rsize = cmdver == 1 ? sizeof(r)
: sizeof(struct ec_response_fp_info_v0);
uint16_t dead;
rv = ec_command(EC_CMD_FP_INFO, cmdver, NULL, 0, &r, rsize);
if (rv < 0)
return rv;
printf("Fingerprint sensor: vendor %x product %x model %x version %x\n",
r.vendor_id, r.product_id, r.model_id, r.version);
printf("Image: size %dx%d %d bpp\n", r.width, r.height, r.bpp);
printf("Error flags: %s%s%s%s\n",
r.errors & FP_ERROR_NO_IRQ ? "NO_IRQ " : "",
r.errors & FP_ERROR_SPI_COMM ? "SPI_COMM " : "",
r.errors & FP_ERROR_BAD_HWID ? "BAD_HWID " : "",
r.errors & FP_ERROR_INIT_FAIL ? "INIT_FAIL " : "");
dead = FP_ERROR_DEAD_PIXELS(r.errors);
if (dead == FP_ERROR_DEAD_PIXELS_UNKNOWN) {
printf("Dead pixels: UNKNOWN\n");
} else {
printf("Dead pixels: %u\n", dead);
}
if (cmdver == 1) {
printf("Templates: version %d size %d count %d/%d"
" dirty bitmap %x\n",
r.template_version, r.template_size, r.template_valid,
r.template_max, r.template_dirty);
}
return 0;
}
int cmd_fp_frame(int argc, char *argv[])
{
struct ec_response_fp_info r;
int idx = (argc == 2 && !strcasecmp(argv[1], "raw")) ?
FP_FRAME_INDEX_RAW_IMAGE : FP_FRAME_INDEX_SIMPLE_IMAGE;
void *buffer = fp_download_frame(&r, idx);
uint8_t *ptr = buffer;
int x, y;
if (!buffer) {
fprintf(stderr, "Failed to get FP sensor frame\n");
return -1;
}
if (idx == FP_FRAME_INDEX_RAW_IMAGE) {
fwrite(buffer, r.frame_size, 1, stdout);
goto frame_done;
}
/* Print 8-bpp PGM ASCII header */
printf("P2\n%d %d\n%d\n", r.width, r.height, (1 << r.bpp) - 1);
for (y = 0; y < r.height; y++) {
for (x = 0; x < r.width; x++, ptr++)
printf("%d ", *ptr);
printf("\n");
}
printf("# END OF FILE\n");
frame_done:
free(buffer);
return 0;
}
int cmd_fp_template(int argc, char *argv[])
{
struct ec_response_fp_info r;
struct ec_params_fp_template *p = ec_outbuf;
/* TODO(b/78544921): removing 32 bits is a workaround for the MCU bug */
int max_chunk = ec_max_outsize
- offsetof(struct ec_params_fp_template, data) - 4;
int idx = -1;
char *e;
int size;
void *buffer = NULL;
uint32_t offset = 0;
int rv = 0;
if (argc < 2) {
fprintf(stderr, "Usage: %s [<infile>|<index>]\n", argv[0]);
return -1;
}
idx = strtol(argv[1], &e, 0);
if (!(e && *e)) {
buffer = fp_download_frame(&r, idx + 1);
if (!buffer) {
fprintf(stderr, "Failed to get FP template %d\n", idx);
return -1;
}
fwrite(buffer, r.template_size, 1, stdout);
free(buffer);
return 0;
}
/* not an index, is it a filename ? */
buffer = read_file(argv[1], &size);
if (!buffer) {
fprintf(stderr, "Invalid parameter: %s\n", argv[1]);
return -1;
}
printf("sending template from: %s (%d bytes)\n", argv[1], size);
while (size) {
uint32_t tlen = MIN(max_chunk, size);
p->offset = offset;
p->size = tlen;
size -= tlen;
if (!size)
p->size |= FP_TEMPLATE_COMMIT;
memcpy(p->data, buffer + offset, tlen);
rv = ec_command(EC_CMD_FP_TEMPLATE, 0, p, tlen +
offsetof(struct ec_params_fp_template, data),
NULL, 0);
if (rv < 0)
break;
offset += tlen;
}
if (rv < 0)
fprintf(stderr, "Failed with %d\n", rv);
else
rv = 0;
free(buffer);
return rv;
}
/**
* determine if in GFU mode or not.
*
* NOTE, Sends HOST commands that modify ec_outbuf contents.
*
* @opos return value of GFU mode object position or zero if not found
* @port port number to query
* @return 1 if in GFU mode, 0 if not, -1 if error
*/
static int in_gfu_mode(int *opos, int port)
{
int i;
struct ec_params_usb_pd_get_mode_request *p =
(struct ec_params_usb_pd_get_mode_request *)ec_outbuf;
struct ec_params_usb_pd_get_mode_response *r =
(struct ec_params_usb_pd_get_mode_response *)ec_inbuf;
p->port = port;
p->svid_idx = 0;
do {
ec_command(EC_CMD_USB_PD_GET_AMODE, 0, p, sizeof(*p),
ec_inbuf, ec_max_insize);
if (!r->svid || (r->svid == USB_VID_GOOGLE))
break;
p->svid_idx++;
} while (p->svid_idx < SVID_DISCOVERY_MAX);
if (r->svid != USB_VID_GOOGLE) {
fprintf(stderr, "Google VID not returned\n");
return -1;
}
*opos = 0; /* invalid ... must be 1 thru 6 */
for (i = 0; i < PDO_MODES; i++) {
if (r->vdo[i] == MODE_GOOGLE_FU) {
*opos = i + 1;
break;
}
}
return r->opos == *opos;
}
/**
* Enter GFU mode.
*
* NOTE, Sends HOST commands that modify ec_outbuf contents.
*
* @port port number to enter GFU on.
* @return 1 if entered GFU mode, 0 if not, -1 if error
*/
static int enter_gfu_mode(int port)
{
int opos;
struct ec_params_usb_pd_set_mode_request *p =
(struct ec_params_usb_pd_set_mode_request *)ec_outbuf;
int gfu_mode = in_gfu_mode(&opos, port);
if (gfu_mode < 0) {
fprintf(stderr, "Failed to query GFU mode support\n");
return 0;
} else if (!gfu_mode) {
if (!opos) {
fprintf(stderr, "Invalid object position %d\n", opos);
return 0;
}
p->port = port;
p->svid = USB_VID_GOOGLE;
p->opos = opos;
p->cmd = PD_ENTER_MODE;
ec_command(EC_CMD_USB_PD_SET_AMODE, 0, p, sizeof(*p),
NULL, 0);
usleep(500000); /* sleep to allow time for set mode */
gfu_mode = in_gfu_mode(&opos, port);
}
return gfu_mode;
}
int cmd_pd_device_info(int argc, char *argv[])
{
int i, rv, port;
char *e;
struct ec_params_usb_pd_info_request *p =
(struct ec_params_usb_pd_info_request *)ec_outbuf;
struct ec_params_usb_pd_rw_hash_entry *r0 =
(struct ec_params_usb_pd_rw_hash_entry *)ec_inbuf;
struct ec_params_usb_pd_discovery_entry *r1;
if (argc < 2) {
fprintf(stderr, "Usage: %s <port>\n", argv[0]);
return -1;
}
port = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad port\n");
return -1;
}
p->port = port;
r1 = (struct ec_params_usb_pd_discovery_entry *)ec_inbuf;
rv = ec_command(EC_CMD_USB_PD_DISCOVERY, 0, p, sizeof(*p),
ec_inbuf, ec_max_insize);
if (rv < 0)
return rv;
if (!r1->vid)
printf("Port:%d has no discovered device\n", port);
else {
printf("Port:%d ptype:%d vid:0x%04x pid:0x%04x\n", port,
r1->ptype, r1->vid, r1->pid);
}
if (enter_gfu_mode(port) != 1) {
fprintf(stderr, "Failed to enter GFU mode\n");
return -1;
}
p->port = port;
rv = ec_command(EC_CMD_USB_PD_DEV_INFO, 0, p, sizeof(*p),
ec_inbuf, ec_max_insize);
if (rv < 0)
return rv;
if (!r0->dev_id)
printf("Port:%d has no valid device\n", port);
else {
uint8_t *rwp = r0->dev_rw_hash;
printf("Port:%d DevId:%d.%d Hash:", port,
HW_DEV_ID_MAJ(r0->dev_id), HW_DEV_ID_MIN(r0->dev_id));
for (i = 0; i < 5; i++) {
printf(" 0x%02x%02x%02x%02x", rwp[3], rwp[2], rwp[1],
rwp[0]);
rwp += 4;
}
printf(" CurImg:%s\n", image_names[r0->current_image]);
}
return rv;
}
int cmd_flash_pd(int argc, char *argv[])
{
struct ec_params_usb_pd_fw_update *p =
(struct ec_params_usb_pd_fw_update *)ec_outbuf;
int i, dev_id, port;
int rv, fsize, step = 96;
char *e;
char *buf;
char *data = (char *)p + sizeof(*p);
if (argc < 4) {
fprintf(stderr, "Usage: %s <dev_id> <port> <filename>\n",
argv[0]);
return -1;
}
dev_id = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad device ID\n");
return -1;
}
port = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad port\n");
return -1;
}
if (enter_gfu_mode(port) != 1) {
fprintf(stderr, "Failed to enter GFU mode\n");
return -1;
}
/* Read the input file */
buf = read_file(argv[3], &fsize);
if (!buf)
return -1;
/* Erase the current RW RSA signature */
fprintf(stderr, "Erasing expected RW hash\n");
p->dev_id = dev_id;
p->port = port;
p->cmd = USB_PD_FW_ERASE_SIG;
p->size = 0;
rv = ec_command(EC_CMD_USB_PD_FW_UPDATE, 0,
p, p->size + sizeof(*p), NULL, 0);
if (rv < 0)
goto pd_flash_error;
/* Reboot */
fprintf(stderr, "Rebooting\n");
p->dev_id = dev_id;
p->port = port;
p->cmd = USB_PD_FW_REBOOT;
p->size = 0;
rv = ec_command(EC_CMD_USB_PD_FW_UPDATE, 0,
p, p->size + sizeof(*p), NULL, 0);
if (rv < 0)
goto pd_flash_error;
usleep(3000000); /* 3sec to reboot and get CC line idle */
/* re-enter GFU after reboot */
if (enter_gfu_mode(port) != 1) {
fprintf(stderr, "Failed to enter GFU mode\n");
goto pd_flash_error;
}
/* Erase RW flash */
fprintf(stderr, "Erasing RW flash\n");
p->dev_id = dev_id;
p->port = port;
p->cmd = USB_PD_FW_FLASH_ERASE;
p->size = 0;
rv = ec_command(EC_CMD_USB_PD_FW_UPDATE, 0,
p, p->size + sizeof(*p), NULL, 0);
/* 3 secs should allow ample time for 2KB page erases at 40ms */
usleep(3000000);
if (rv < 0)
goto pd_flash_error;
/* Write RW flash */
fprintf(stderr, "Writing RW flash\n");
p->dev_id = dev_id;
p->port = port;
p->cmd = USB_PD_FW_FLASH_WRITE;
p->size = step;
for (i = 0; i < fsize; i += step) {
p->size = MIN(fsize - i, step);
memcpy(data, buf + i, p->size);
rv = ec_command(EC_CMD_USB_PD_FW_UPDATE, 0,
p, p->size + sizeof(*p), NULL, 0);
if (rv < 0)
goto pd_flash_error;
/*
* TODO(crosbug.com/p/33905) throttle so EC doesn't watchdog on
* other tasks. Remove once issue resolved.
*/
usleep(10000);
}
/* 100msec to guarantee writes finish */
usleep(100000);
/* Reboot into new RW */
fprintf(stderr, "Rebooting PD into new RW\n");
p->cmd = USB_PD_FW_REBOOT;
p->size = 0;
rv = ec_command(EC_CMD_USB_PD_FW_UPDATE, 0,
p, p->size + sizeof(*p), NULL, 0);
if (rv < 0)
goto pd_flash_error;
free(buf);
fprintf(stderr, "Complete\n");
return 0;
pd_flash_error:
free(buf);
fprintf(stderr, "PD flash error\n");
return -1;
}
int cmd_pd_set_amode(int argc, char *argv[])
{
char *e;
struct ec_params_usb_pd_set_mode_request *p =
(struct ec_params_usb_pd_set_mode_request *)ec_outbuf;
if (argc < 5) {
fprintf(stderr, "Usage: %s <port> <svid> <opos> <cmd>\n",
argv[0]);
return -1;
}
p->port = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad port\n");
return -1;
}
p->svid = strtol(argv[2], &e, 0);
if ((e && *e) || !p->svid) {
fprintf(stderr, "Bad svid\n");
return -1;
}
p->opos = strtol(argv[3], &e, 0);
if ((e && *e) || !p->opos) {
fprintf(stderr, "Bad opos\n");
return -1;
}
p->cmd = strtol(argv[4], &e, 0);
if ((e && *e) || (p->cmd >= PD_MODE_CMD_COUNT)) {
fprintf(stderr, "Bad cmd\n");
return -1;
}
return ec_command(EC_CMD_USB_PD_SET_AMODE, 0, p, sizeof(*p), NULL, 0);
}
int cmd_pd_get_amode(int argc, char *argv[])
{
int i;
char *e;
struct ec_params_usb_pd_get_mode_request *p =
(struct ec_params_usb_pd_get_mode_request *)ec_outbuf;
struct ec_params_usb_pd_get_mode_response *r =
(struct ec_params_usb_pd_get_mode_response *)ec_inbuf;
if (argc < 2) {
fprintf(stderr, "Usage: %s <port>\n", argv[0]);
return -1;
}
p->port = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad port\n");
return -1;
}
p->svid_idx = 0;
do {
ec_command(EC_CMD_USB_PD_GET_AMODE, 0, p, sizeof(*p),
ec_inbuf, ec_max_insize);
if (!r->svid)
break;
printf("%cSVID:0x%04x ", (r->opos) ? '*' : ' ',
r->svid);
for (i = 0; i < PDO_MODES; i++) {
printf("%c0x%08x ", (r->opos && (r->opos == i + 1)) ?
'*' : ' ', r->vdo[i]);
}
printf("\n");
p->svid_idx++;
} while (p->svid_idx < SVID_DISCOVERY_MAX);
return -1;
}
/* The I/O asm funcs exist only on x86. */
#if defined(__i386__) || defined(__x86_64__)
#include <sys/io.h>
int cmd_serial_test(int argc, char *argv[])
{
const char *c = "COM2 sample serial output from host!\r\n";
printf("Writing sample serial output to COM2\n");
while (*c) {
/* Wait for space in transmit FIFO */
while (!(inb(0x2fd) & 0x20))
;
/* Put the next character */
outb(*c++, 0x2f8);
}
printf("done.\n");
return 0;
}
int cmd_port_80_flood(int argc, char *argv[])
{
int i;
for (i = 0; i < 256; i++)
outb(i, 0x80);
return 0;
}
#else
int cmd_serial_test(int argc, char *argv[])
{
printf("x86 specific command\n");
return -1;
}
int cmd_port_80_flood(int argc, char *argv[])
{
printf("x86 specific command\n");
return -1;
}
#endif
int read_mapped_temperature(int id)
{
int rv;
if (!read_mapped_mem8(EC_MEMMAP_THERMAL_VERSION)) {
/*
* The temp_sensor_init() is not called, which implies no
* temp sensor is defined.
*/
rv = EC_TEMP_SENSOR_NOT_PRESENT;
} else if (id < EC_TEMP_SENSOR_ENTRIES)
rv = read_mapped_mem8(EC_MEMMAP_TEMP_SENSOR + id);
else if (read_mapped_mem8(EC_MEMMAP_THERMAL_VERSION) >= 2)
rv = read_mapped_mem8(EC_MEMMAP_TEMP_SENSOR_B +
id - EC_TEMP_SENSOR_ENTRIES);
else {
/* Sensor in second bank, but second bank isn't supported */
rv = EC_TEMP_SENSOR_NOT_PRESENT;
}
return rv;
}
int cmd_temperature(int argc, char *argv[])
{
int rv;
int id;
char *e;
if (argc != 2) {
fprintf(stderr, "Usage: %s <sensorid> | all\n", argv[0]);
return -1;
}
if (strcmp(argv[1], "all") == 0) {
for (id = 0;
id < EC_TEMP_SENSOR_ENTRIES + EC_TEMP_SENSOR_B_ENTRIES;
id++) {
rv = read_mapped_temperature(id);
switch (rv) {
case EC_TEMP_SENSOR_NOT_PRESENT:
break;
case EC_TEMP_SENSOR_ERROR:
fprintf(stderr, "Sensor %d error\n", id);
break;
case EC_TEMP_SENSOR_NOT_POWERED:
fprintf(stderr, "Sensor %d disabled\n", id);
break;
case EC_TEMP_SENSOR_NOT_CALIBRATED:
fprintf(stderr, "Sensor %d not calibrated\n",
id);
break;
default:
printf("%d: %d K\n", id,
rv + EC_TEMP_SENSOR_OFFSET);
}
}
return 0;
}
id = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad sensor ID.\n");
return -1;
}
if (id < 0 ||
id >= EC_TEMP_SENSOR_ENTRIES + EC_TEMP_SENSOR_B_ENTRIES) {
printf("Sensor ID invalid.\n");
return -1;
}
printf("Reading temperature...");
rv = read_mapped_temperature(id);
switch (rv) {
case EC_TEMP_SENSOR_NOT_PRESENT:
printf("Sensor not present\n");
return -1;
case EC_TEMP_SENSOR_ERROR:
printf("Error\n");
return -1;
case EC_TEMP_SENSOR_NOT_POWERED:
printf("Sensor disabled/unpowered\n");
return -1;
case EC_TEMP_SENSOR_NOT_CALIBRATED:
fprintf(stderr, "Sensor not calibrated\n");
return -1;
default:
printf("%d K\n", rv + EC_TEMP_SENSOR_OFFSET);
return 0;
}
}
int cmd_temp_sensor_info(int argc, char *argv[])
{
struct ec_params_temp_sensor_get_info p;
struct ec_response_temp_sensor_get_info r;
int rv;
char *e;
if (argc != 2) {
fprintf(stderr, "Usage: %s <sensorid> | all\n", argv[0]);
return -1;
}
if (strcmp(argv[1], "all") == 0) {
for (p.id = 0;
p.id < EC_TEMP_SENSOR_ENTRIES + EC_TEMP_SENSOR_B_ENTRIES;
p.id++) {
if (read_mapped_temperature(p.id) ==
EC_TEMP_SENSOR_NOT_PRESENT)
continue;
rv = ec_command(EC_CMD_TEMP_SENSOR_GET_INFO, 0,
&p, sizeof(p), &r, sizeof(r));
if (rv < 0)
continue;
printf("%d: %d %s\n", p.id, r.sensor_type,
r.sensor_name);
}
return 0;
}
p.id = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad sensor ID.\n");
return -1;
}
rv = ec_command(EC_CMD_TEMP_SENSOR_GET_INFO, 0,
&p, sizeof(p), &r, sizeof(r));
if (rv < 0)
return rv;
printf("Sensor name: %s\n", r.sensor_name);
printf("Sensor type: %d\n", r.sensor_type);
return 0;
}
int cmd_thermal_get_threshold_v0(int argc, char *argv[])
{
struct ec_params_thermal_get_threshold p;
struct ec_response_thermal_get_threshold r;
char *e;
int rv;
if (argc != 3) {
fprintf(stderr,
"Usage: %s <sensortypeid> <thresholdid>\n", argv[0]);
return -1;
}
p.sensor_type = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad sensor type ID.\n");
return -1;
}
p.threshold_id = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad threshold ID.\n");
return -1;
}
rv = ec_command(EC_CMD_THERMAL_GET_THRESHOLD, 0,
&p, sizeof(p), &r, sizeof(r));
if (rv < 0)
return rv;
printf("Threshold %d for sensor type %d is %d K.\n",
p.threshold_id, p.sensor_type, r.value);
return 0;
}
int cmd_thermal_set_threshold_v0(int argc, char *argv[])
{
struct ec_params_thermal_set_threshold p;
char *e;
int rv;
if (argc != 4) {
fprintf(stderr,
"Usage: %s <sensortypeid> <thresholdid> <value>\n",
argv[0]);
return -1;
}
p.sensor_type = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad sensor type ID.\n");
return -1;
}
p.threshold_id = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad threshold ID.\n");
return -1;
}
p.value = strtol(argv[3], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad threshold value.\n");
return -1;
}
rv = ec_command(EC_CMD_THERMAL_SET_THRESHOLD, 0,
&p, sizeof(p), NULL, 0);
if (rv < 0)
return rv;
printf("Threshold %d for sensor type %d set to %d.\n",
p.threshold_id, p.sensor_type, p.value);
return 0;
}
int cmd_thermal_get_threshold_v1(int argc, char *argv[])
{
struct ec_params_thermal_get_threshold_v1 p;
struct ec_thermal_config r;
struct ec_params_temp_sensor_get_info pi;
struct ec_response_temp_sensor_get_info ri;
int rv;
int i;
printf("sensor warn high halt fan_off fan_max name\n");
for (i = 0; i < 99; i++) { /* number of sensors is unknown */
/* ask for one */
p.sensor_num = i;
rv = ec_command(EC_CMD_THERMAL_GET_THRESHOLD, 1,
&p, sizeof(p), &r, sizeof(r));
if (rv <= 0) /* stop on first failure */
break;
/* ask for its name, too */
pi.id = i;
rv = ec_command(EC_CMD_TEMP_SENSOR_GET_INFO, 0,
&pi, sizeof(pi), &ri, sizeof(ri));
/* print what we know */
printf(" %2d %3d %3d %3d %3d %3d %s\n",
i,
r.temp_host[EC_TEMP_THRESH_WARN],
r.temp_host[EC_TEMP_THRESH_HIGH],
r.temp_host[EC_TEMP_THRESH_HALT],
r.temp_fan_off, r.temp_fan_max,
rv > 0 ? ri.sensor_name : "?");
}
if (i)
printf("(all temps in degrees Kelvin)\n");
return 0;
}
int cmd_thermal_set_threshold_v1(int argc, char *argv[])
{
struct ec_params_thermal_get_threshold_v1 p;
struct ec_thermal_config r;
struct ec_params_thermal_set_threshold_v1 s;
int i, n, val, rv;
char *e;
if (argc < 3 || argc > 7) {
printf("Usage: %s"
" sensor warn [high [shutdown [fan_off [fan_max]]]]\n",
argv[0]);
return 1;
}
n = strtod(argv[1], &e);
if (e && *e) {
printf("arg %d is invalid\n", 1);
return 1;
}
p.sensor_num = n;
rv = ec_command(EC_CMD_THERMAL_GET_THRESHOLD, 1,
&p, sizeof(p), &r, sizeof(r));
if (rv <= 0)
return rv;
s.sensor_num = n;
s.cfg = r;
for (i = 2; i < argc; i++) {
val = strtod(argv[i], &e);
if (e && *e) {
printf("arg %d is invalid\n", i);
return 1;
}
if (val < 0)
continue;
switch (i) {
case 2:
case 3:
case 4:
s.cfg.temp_host[i-2] = val;
break;
case 5:
s.cfg.temp_fan_off = val;
break;
case 6:
s.cfg.temp_fan_max = val;
break;
}
}
rv = ec_command(EC_CMD_THERMAL_SET_THRESHOLD, 1,
&s, sizeof(s), NULL, 0);
return rv;
}
int cmd_thermal_get_threshold(int argc, char *argv[])
{
if (ec_cmd_version_supported(EC_CMD_THERMAL_GET_THRESHOLD, 1))
return cmd_thermal_get_threshold_v1(argc, argv);
else if (ec_cmd_version_supported(EC_CMD_THERMAL_GET_THRESHOLD, 0))
return cmd_thermal_get_threshold_v0(argc, argv);
printf("I got nuthin.\n");
return -1;
}
int cmd_thermal_set_threshold(int argc, char *argv[])
{
if (ec_cmd_version_supported(EC_CMD_THERMAL_SET_THRESHOLD, 1))
return cmd_thermal_set_threshold_v1(argc, argv);
else if (ec_cmd_version_supported(EC_CMD_THERMAL_SET_THRESHOLD, 0))
return cmd_thermal_set_threshold_v0(argc, argv);
printf("I got nuthin.\n");
return -1;
}
static int get_num_fans(void)
{
int idx, rv;
struct ec_response_get_features r;
/*
* iff the EC supports the GET_FEATURES,
* check whether it has fan support enabled.
*/
rv = ec_command(EC_CMD_GET_FEATURES, 0, NULL, 0, &r, sizeof(r));
if (rv >= 0 && !(r.flags[0] & BIT(EC_FEATURE_PWM_FAN)))
return 0;
for (idx = 0; idx < EC_FAN_SPEED_ENTRIES; idx++) {
rv = read_mapped_mem16(EC_MEMMAP_FAN + 2 * idx);
if (rv == EC_FAN_SPEED_NOT_PRESENT)
break;
}
return idx;
}
int cmd_thermal_auto_fan_ctrl(int argc, char *argv[])
{
int rv, num_fans;
struct ec_params_auto_fan_ctrl_v1 p_v1;
char *e;
int cmdver = 1;
if (!ec_cmd_version_supported(EC_CMD_THERMAL_AUTO_FAN_CTRL, cmdver)
|| (argc == 1)) {
/* If no argument is provided then enable auto fan ctrl */
/* for all fans by using version 0 of the host command */
rv = ec_command(EC_CMD_THERMAL_AUTO_FAN_CTRL, 0,
NULL, 0, NULL, 0);
if (rv < 0)
return rv;
printf("Automatic fan control is now on for all fans.\n");
return 0;
}
if (argc > 2 || !strcmp(argv[1], "help")) {
printf("Usage: %s [idx]\n", argv[0]);
return -1;
}
num_fans = get_num_fans();
p_v1.fan_idx = strtol(argv[1], &e, 0);
if ((e && *e) || (p_v1.fan_idx >= num_fans)) {
fprintf(stderr, "Bad fan index.\n");
return -1;
}
rv = ec_command(EC_CMD_THERMAL_AUTO_FAN_CTRL, cmdver,
&p_v1, sizeof(p_v1), NULL, 0);
if (rv < 0)
return rv;
printf("Automatic fan control is now on for fan %d\n", p_v1.fan_idx);
return 0;
}
static int print_fan(int idx)
{
int rv = read_mapped_mem16(EC_MEMMAP_FAN + 2 * idx);
switch (rv) {
case EC_FAN_SPEED_NOT_PRESENT:
return -1;
case EC_FAN_SPEED_STALLED:
printf("Fan %d stalled!\n", idx);
break;
default:
printf("Fan %d RPM: %d\n", idx, rv);
break;
}
return 0;
}
int cmd_pwm_get_num_fans(int argc, char *argv[])
{
int num_fans;
num_fans = get_num_fans();
printf("Number of fans = %d\n", num_fans);
return 0;
}
int cmd_pwm_get_fan_rpm(int argc, char *argv[])
{
int i, num_fans;
num_fans = get_num_fans();
if (argc < 2 || !strcasecmp(argv[1], "all")) {
/* Print all the fan speeds */
for (i = 0; i < num_fans; i++)
print_fan(i);
} else {
char *e;
int idx;
idx = strtol(argv[1], &e, 0);
if ((e && *e) || idx < 0 || idx >= num_fans) {
fprintf(stderr, "Bad index.\n");
return -1;
}
print_fan(idx);
}
return 0;
}
int cmd_pwm_set_fan_rpm(int argc, char *argv[])
{
struct ec_params_pwm_set_fan_target_rpm_v1 p_v1;
char *e;
int rv, num_fans;
int cmdver = 1;
if (!ec_cmd_version_supported(EC_CMD_PWM_SET_FAN_TARGET_RPM, cmdver)) {
struct ec_params_pwm_set_fan_target_rpm_v0 p_v0;
/* Fall back to command version 0 command */
cmdver = 0;
if (argc != 2) {
fprintf(stderr,
"Usage: %s <targetrpm>\n", argv[0]);
return -1;
}
p_v0.rpm = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad RPM.\n");
return -1;
}
rv = ec_command(EC_CMD_PWM_SET_FAN_TARGET_RPM, cmdver,
&p_v0, sizeof(p_v0), NULL, 0);
if (rv < 0)
return rv;
printf("Fan target RPM set for all fans.\n");
return 0;
}
if (argc > 3 || (argc == 2 && !strcmp(argv[1], "help")) || argc == 1) {
printf("Usage: %s [idx] <targetrpm>\n", argv[0]);
printf("'%s 0 3000' - Set fan 0 RPM to 3000\n", argv[0]);
printf("'%s 3000' - Set all fans RPM to 3000\n", argv[0]);
return -1;
}
num_fans = get_num_fans();
p_v1.rpm = strtol(argv[argc - 1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad RPM.\n");
return -1;
}
if (argc == 2) {
/* Reuse version 0 command if we're setting targetrpm
* for all fans */
struct ec_params_pwm_set_fan_target_rpm_v0 p_v0;
cmdver = 0;
p_v0.rpm = p_v1.rpm;
rv = ec_command(EC_CMD_PWM_SET_FAN_TARGET_RPM, cmdver,
&p_v0, sizeof(p_v0), NULL, 0);
if (rv < 0)
return rv;
printf("Fan target RPM set for all fans.\n");
} else {
p_v1.fan_idx = strtol(argv[1], &e, 0);
if ((e && *e) || (p_v1.fan_idx >= num_fans)) {
fprintf(stderr, "Bad fan index.\n");
return -1;
}
rv = ec_command(EC_CMD_PWM_SET_FAN_TARGET_RPM, cmdver,
&p_v1, sizeof(p_v1), NULL, 0);
if (rv < 0)
return rv;
printf("Fan %d target RPM set.\n", p_v1.fan_idx);
}
return 0;
}
int cmd_pwm_get_keyboard_backlight(int argc, char *argv[])
{
struct ec_response_pwm_get_keyboard_backlight r;
int rv;
rv = ec_command(EC_CMD_PWM_GET_KEYBOARD_BACKLIGHT, 0,
NULL, 0, &r, sizeof(r));
if (rv < 0)
return rv;
if (r.enabled == 1)
printf("Current keyboard backlight percent: %d\n", r.percent);
else
printf("Keyboard backlight disabled.\n");
return 0;
}
int cmd_pwm_set_keyboard_backlight(int argc, char *argv[])
{
struct ec_params_pwm_set_keyboard_backlight p;
char *e;
int rv;
if (argc != 2) {
fprintf(stderr, "Usage: %s <percent>\n", argv[0]);
return -1;
}
p.percent = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad percent.\n");
return -1;
}
rv = ec_command(EC_CMD_PWM_SET_KEYBOARD_BACKLIGHT, 0,
&p, sizeof(p), NULL, 0);
if (rv < 0)
return rv;
printf("Keyboard backlight set.\n");
return 0;
}
int cmd_pwm_get_duty(int argc, char *argv[])
{
struct ec_params_pwm_get_duty p;
struct ec_response_pwm_get_duty r;
char *e;
int rv;
if (argc != 2) {
fprintf(stderr, "Usage: %s <pwm_idx> | kb | disp\n", argv[0]);
return -1;
}
if (!strcmp(argv[1], "kb")) {
p.pwm_type = EC_PWM_TYPE_KB_LIGHT;
p.index = 0;
} else if (!strcmp(argv[1], "disp")) {
p.pwm_type = EC_PWM_TYPE_DISPLAY_LIGHT;
p.index = 0;
} else {
p.pwm_type = EC_PWM_TYPE_GENERIC;
p.index = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad pwm_idx\n");
return -1;
}
}
rv = ec_command(EC_CMD_PWM_GET_DUTY, 0, &p, sizeof(p), &r, sizeof(r));
if (rv < 0)
return rv;
printf("Current PWM duty: %d\n", r.duty);
return 0;
}
int cmd_pwm_set_duty(int argc, char *argv[])
{
struct ec_params_pwm_set_duty p;
char *e;
int rv;
if (argc != 3) {
fprintf(stderr, "Usage: %s <pwm_idx> | kb | disp <duty>\n",
argv[0]);
return -1;
}
if (!strcmp(argv[1], "kb")) {
p.pwm_type = EC_PWM_TYPE_KB_LIGHT;
p.index = 0;
} else if (!strcmp(argv[1], "disp")) {
p.pwm_type = EC_PWM_TYPE_DISPLAY_LIGHT;
p.index = 0;
} else {
p.pwm_type = EC_PWM_TYPE_GENERIC;
p.index = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad pwm_idx\n");
return -1;
}
}
p.duty = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad duty.\n");
return -1;
}
rv = ec_command(EC_CMD_PWM_SET_DUTY, 0,
&p, sizeof(p), NULL, 0);
if (rv < 0)
return rv;
printf("PWM set.\n");
return 0;
}
int cmd_fanduty(int argc, char *argv[])
{
struct ec_params_pwm_set_fan_duty_v1 p_v1;
char *e;
int rv, num_fans;
int cmdver = 1;
if (!ec_cmd_version_supported(EC_CMD_PWM_SET_FAN_DUTY, cmdver)) {
struct ec_params_pwm_set_fan_duty_v0 p_v0;
if (argc != 2) {
fprintf(stderr,
"Usage: %s <percent>\n", argv[0]);
return -1;
}
p_v0.percent = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad percent arg.\n");
return -1;
}
rv = ec_command(EC_CMD_PWM_SET_FAN_DUTY, 0,
&p_v0, sizeof(p_v0), NULL, 0);
if (rv < 0)
return rv;
printf("Fan duty cycle set.\n");
return 0;
}
if (argc > 3 || (argc == 2 && !strcmp(argv[1], "help")) || argc == 1) {
printf("Usage: %s [idx] <percent>\n", argv[0]);
printf("'%s 0 50' - Set fan 0 duty cycle to 50 percent\n",
argv[0]);
printf("'%s 30' - Set all fans duty cycle to 30 percent\n",
argv[0]);
return -1;
}
num_fans = get_num_fans();
p_v1.percent = strtol(argv[argc - 1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad percent arg.\n");
return -1;
}
if (argc == 2) {
/* Reuse version 0 command if we're setting duty cycle
* for all fans */
struct ec_params_pwm_set_fan_duty_v0 p_v0;
cmdver = 0;
p_v0.percent = p_v1.percent;
rv = ec_command(EC_CMD_PWM_SET_FAN_DUTY, cmdver,
&p_v0, sizeof(p_v0), NULL, 0);
if (rv < 0)
return rv;
printf("Fan duty cycle set for all fans.\n");
} else {
p_v1.fan_idx = strtol(argv[1], &e, 0);
if ((e && *e) || (p_v1.fan_idx >= num_fans)) {
fprintf(stderr, "Bad fan index.\n");
return -1;
}
rv = ec_command(EC_CMD_PWM_SET_FAN_DUTY, cmdver,
&p_v1, sizeof(p_v1), NULL, 0);
if (rv < 0)
return rv;
printf("Fan %d duty cycle set.\n", p_v1.fan_idx);
}
return 0;
}
#define LBMSG(state) #state
#include "lightbar_msg_list.h"
static const char * const lightbar_cmds[] = {
LIGHTBAR_MSG_LIST
};
#undef LBMSG
/* Size of field <FLD> in structure <ST> */
#define ST_FLD_SIZE(ST, FLD) sizeof(((struct ST *)0)->FLD)
#define ST_CMD_SIZE ST_FLD_SIZE(ec_params_lightbar, cmd)
#define ST_PRM_SIZE(SUBCMD) \
(ST_CMD_SIZE + ST_FLD_SIZE(ec_params_lightbar, SUBCMD))
#define ST_RSP_SIZE(SUBCMD) ST_FLD_SIZE(ec_response_lightbar, SUBCMD)
static const struct {
uint8_t insize;
uint8_t outsize;
} lb_command_paramcount[] = {
{ ST_CMD_SIZE, ST_RSP_SIZE(dump) },
{ ST_CMD_SIZE, 0 },
{ ST_CMD_SIZE, 0 },
{ ST_CMD_SIZE, 0 },
{ ST_PRM_SIZE(set_brightness), 0},
{ ST_PRM_SIZE(seq), 0},
{ ST_PRM_SIZE(reg), 0},
{ ST_PRM_SIZE(set_rgb), 0},
{ ST_CMD_SIZE, ST_RSP_SIZE(get_seq) },
{ ST_PRM_SIZE(demo), 0},
{ ST_CMD_SIZE, ST_RSP_SIZE(get_params_v0) },
{ ST_PRM_SIZE(set_params_v0), 0},
{ ST_CMD_SIZE, ST_RSP_SIZE(version) },
{ ST_CMD_SIZE, ST_RSP_SIZE(get_brightness) },
{ ST_PRM_SIZE(get_rgb), ST_RSP_SIZE(get_rgb) },
{ ST_CMD_SIZE, ST_RSP_SIZE(get_demo) },
{ ST_CMD_SIZE, ST_RSP_SIZE(get_params_v1) },
{ ST_PRM_SIZE(set_params_v1), 0},
{ ST_PRM_SIZE(set_program), 0},
{ ST_PRM_SIZE(manual_suspend_ctrl), 0},
{ ST_CMD_SIZE, 0 },
{ ST_CMD_SIZE, 0 },
{ ST_CMD_SIZE, ST_RSP_SIZE(get_params_v2_timing) },
{ ST_PRM_SIZE(set_v2par_timing), 0},
{ ST_CMD_SIZE, ST_RSP_SIZE(get_params_v2_tap) },
{ ST_PRM_SIZE(set_v2par_tap), 0},
{ ST_CMD_SIZE, ST_RSP_SIZE(get_params_v2_osc) },
{ ST_PRM_SIZE(set_v2par_osc), 0},
{ ST_CMD_SIZE, ST_RSP_SIZE(get_params_v2_bright) },
{ ST_PRM_SIZE(set_v2par_bright), 0},
{ ST_CMD_SIZE, ST_RSP_SIZE(get_params_v2_thlds) },
{ ST_PRM_SIZE(set_v2par_thlds), 0},
{ ST_CMD_SIZE, ST_RSP_SIZE(get_params_v2_colors) },
{ ST_PRM_SIZE(set_v2par_colors), 0},
};
BUILD_ASSERT(ARRAY_SIZE(lb_command_paramcount) == LIGHTBAR_NUM_CMDS);
#undef ST_CMD_SIZE
#undef ST_PRM_SIZE
#undef ST_RSP_SIZE
static int lb_help(const char *cmd)
{
printf("Usage:\n");
printf(" %s - dump all regs\n", cmd);
printf(" %s off - enter standby\n", cmd);
printf(" %s on - leave standby\n", cmd);
printf(" %s init - load default vals\n", cmd);
printf(" %s brightness [NUM] - get/set intensity(0-ff)\n", cmd);
printf(" %s seq [NUM|SEQUENCE] - run given pattern"
" (no arg for list)\n", cmd);
printf(" %s CTRL REG VAL - set LED controller regs\n", cmd);
printf(" %s LED RED GREEN BLUE - set color manually"
" (LED=4 for all)\n", cmd);
printf(" %s LED - get current LED color\n", cmd);
printf(" %s demo [0|1] - turn demo mode on & off\n", cmd);
printf(" %s params [setfile] - get params"
" (or set from file)\n", cmd);
printf(" %s params2 group [setfile] - get params by group\n"
" (or set from file)\n", cmd);
printf(" %s program file - load program from file\n", cmd);
return 0;
}
static uint8_t lb_find_msg_by_name(const char *str)
{
uint8_t i;
for (i = 0; i < LIGHTBAR_NUM_SEQUENCES; i++)
if (!strcasecmp(str, lightbar_cmds[i]))
return i;
return LIGHTBAR_NUM_SEQUENCES;
}
static int lb_do_cmd(enum lightbar_command cmd,
struct ec_params_lightbar *in,
struct ec_response_lightbar *out)
{
int rv;
in->cmd = cmd;
rv = ec_command(EC_CMD_LIGHTBAR_CMD, 0,
in, lb_command_paramcount[cmd].insize,
out, lb_command_paramcount[cmd].outsize);
return (rv < 0 ? rv : 0);
}
static int lb_show_msg_names(void)
{
int i, current_state;
struct ec_params_lightbar param;
struct ec_response_lightbar resp;
i = lb_do_cmd(LIGHTBAR_CMD_GET_SEQ, ¶m, &resp);
if (i < 0)
return i;
current_state = resp.get_seq.num;
printf("sequence names:");
for (i = 0; i < LIGHTBAR_NUM_SEQUENCES; i++)
printf(" %s", lightbar_cmds[i]);
printf("\nCurrent = 0x%x %s\n", current_state,
lightbar_cmds[current_state]);
return 0;
}
static int lb_read_params_v0_from_file(const char *filename,
struct lightbar_params_v0 *p)
{
FILE *fp;
char buf[80];
int val[4];
int r = 1;
int line = 0;
int want, got;
int i;
fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "Can't open %s: %s\n",
filename, strerror(errno));
return 1;
}
/* We must read the correct number of params from each line */
#define READ(N) do { \
line++; \
want = (N); \
got = -1; \
if (!fgets(buf, sizeof(buf), fp)) \
goto done; \
got = sscanf(buf, "%i %i %i %i", \
&val[0], &val[1], &val[2], &val[3]); \
if (want != got) \
goto done; \
} while (0)
/* Do it */
READ(1); p->google_ramp_up = val[0];
READ(1); p->google_ramp_down = val[0];
READ(1); p->s3s0_ramp_up = val[0];
READ(1); p->s0_tick_delay[0] = val[0];
READ(1); p->s0_tick_delay[1] = val[0];
READ(1); p->s0a_tick_delay[0] = val[0];
READ(1); p->s0a_tick_delay[1] = val[0];
READ(1); p->s0s3_ramp_down = val[0];
READ(1); p->s3_sleep_for = val[0];
READ(1); p->s3_ramp_up = val[0];
READ(1); p->s3_ramp_down = val[0];
READ(1); p->new_s0 = val[0];
READ(2);
p->osc_min[0] = val[0];
p->osc_min[1] = val[1];
READ(2);
p->osc_max[0] = val[0];
p->osc_max[1] = val[1];
READ(2);
p->w_ofs[0] = val[0];
p->w_ofs[1] = val[1];
READ(2);
p->bright_bl_off_fixed[0] = val[0];
p->bright_bl_off_fixed[1] = val[1];
READ(2);
p->bright_bl_on_min[0] = val[0];
p->bright_bl_on_min[1] = val[1];
READ(2);
p->bright_bl_on_max[0] = val[0];
p->bright_bl_on_max[1] = val[1];
READ(3);
p->battery_threshold[0] = val[0];
p->battery_threshold[1] = val[1];
p->battery_threshold[2] = val[2];
READ(4);
p->s0_idx[0][0] = val[0];
p->s0_idx[0][1] = val[1];
p->s0_idx[0][2] = val[2];
p->s0_idx[0][3] = val[3];
READ(4);
p->s0_idx[1][0] = val[0];
p->s0_idx[1][1] = val[1];
p->s0_idx[1][2] = val[2];
p->s0_idx[1][3] = val[3];
READ(4);
p->s3_idx[0][0] = val[0];
p->s3_idx[0][1] = val[1];
p->s3_idx[0][2] = val[2];
p->s3_idx[0][3] = val[3];
READ(4);
p->s3_idx[1][0] = val[0];
p->s3_idx[1][1] = val[1];
p->s3_idx[1][2] = val[2];
p->s3_idx[1][3] = val[3];
for (i = 0; i < ARRAY_SIZE(p->color); i++) {
READ(3);
p->color[i].r = val[0];
p->color[i].g = val[1];
p->color[i].b = val[2];
}
#undef READ
/* Yay */
r = 0;
done:
if (r)
fprintf(stderr, "problem with line %d: wanted %d, got %d\n",
line, want, got);
fclose(fp);
return r;
}
static void lb_show_params_v0(const struct lightbar_params_v0 *p)
{
int i;
printf("%d\t\t# .google_ramp_up\n", p->google_ramp_up);
printf("%d\t\t# .google_ramp_down\n", p->google_ramp_down);
printf("%d\t\t# .s3s0_ramp_up\n", p->s3s0_ramp_up);
printf("%d\t\t# .s0_tick_delay (battery)\n", p->s0_tick_delay[0]);
printf("%d\t\t# .s0_tick_delay (AC)\n", p->s0_tick_delay[1]);
printf("%d\t\t# .s0a_tick_delay (battery)\n", p->s0a_tick_delay[0]);
printf("%d\t\t# .s0a_tick_delay (AC)\n", p->s0a_tick_delay[1]);
printf("%d\t\t# .s0s3_ramp_down\n", p->s0s3_ramp_down);
printf("%d\t# .s3_sleep_for\n", p->s3_sleep_for);
printf("%d\t\t# .s3_ramp_up\n", p->s3_ramp_up);
printf("%d\t\t# .s3_ramp_down\n", p->s3_ramp_down);
printf("%d\t\t# .new_s0\n", p->new_s0);
printf("0x%02x 0x%02x\t# .osc_min (battery, AC)\n",
p->osc_min[0], p->osc_min[1]);
printf("0x%02x 0x%02x\t# .osc_max (battery, AC)\n",
p->osc_max[0], p->osc_max[1]);
printf("%d %d\t\t# .w_ofs (battery, AC)\n",
p->w_ofs[0], p->w_ofs[1]);
printf("0x%02x 0x%02x\t# .bright_bl_off_fixed (battery, AC)\n",
p->bright_bl_off_fixed[0], p->bright_bl_off_fixed[1]);
printf("0x%02x 0x%02x\t# .bright_bl_on_min (battery, AC)\n",
p->bright_bl_on_min[0], p->bright_bl_on_min[1]);
printf("0x%02x 0x%02x\t# .bright_bl_on_max (battery, AC)\n",
p->bright_bl_on_max[0], p->bright_bl_on_max[1]);
printf("%d %d %d\t\t# .battery_threshold\n",
p->battery_threshold[0],
p->battery_threshold[1],
p->battery_threshold[2]);
printf("%d %d %d %d\t\t# .s0_idx[] (battery)\n",
p->s0_idx[0][0], p->s0_idx[0][1],
p->s0_idx[0][2], p->s0_idx[0][3]);
printf("%d %d %d %d\t\t# .s0_idx[] (AC)\n",
p->s0_idx[1][0], p->s0_idx[1][1],
p->s0_idx[1][2], p->s0_idx[1][3]);
printf("%d %d %d %d\t# .s3_idx[] (battery)\n",
p->s3_idx[0][0], p->s3_idx[0][1],
p->s3_idx[0][2], p->s3_idx[0][3]);
printf("%d %d %d %d\t# .s3_idx[] (AC)\n",
p->s3_idx[1][0], p->s3_idx[1][1],
p->s3_idx[1][2], p->s3_idx[1][3]);
for (i = 0; i < ARRAY_SIZE(p->color); i++)
printf("0x%02x 0x%02x 0x%02x\t# color[%d]\n",
p->color[i].r,
p->color[i].g,
p->color[i].b, i);
}
static int lb_read_params_v1_from_file(const char *filename,
struct lightbar_params_v1 *p)
{
FILE *fp;
char buf[80];
int val[4];
int r = 1;
int line = 0;
int want, got;
int i;
fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "Can't open %s: %s\n",
filename, strerror(errno));
return 1;
}
/* We must read the correct number of params from each line */
#define READ(N) do { \
line++; \
want = (N); \
got = -1; \
if (!fgets(buf, sizeof(buf), fp)) \
goto done; \
got = sscanf(buf, "%i %i %i %i", \
&val[0], &val[1], &val[2], &val[3]); \
if (want != got) \
goto done; \
} while (0)
/* Do it */
READ(1); p->google_ramp_up = val[0];
READ(1); p->google_ramp_down = val[0];
READ(1); p->s3s0_ramp_up = val[0];
READ(1); p->s0_tick_delay[0] = val[0];
READ(1); p->s0_tick_delay[1] = val[0];
READ(1); p->s0a_tick_delay[0] = val[0];
READ(1); p->s0a_tick_delay[1] = val[0];
READ(1); p->s0s3_ramp_down = val[0];
READ(1); p->s3_sleep_for = val[0];
READ(1); p->s3_ramp_up = val[0];
READ(1); p->s3_ramp_down = val[0];
READ(1); p->tap_tick_delay = val[0];
READ(1); p->tap_gate_delay = val[0];
READ(1); p->tap_display_time = val[0];
READ(1); p->tap_pct_red = val[0];
READ(1); p->tap_pct_green = val[0];
READ(1); p->tap_seg_min_on = val[0];
READ(1); p->tap_seg_max_on = val[0];
READ(1); p->tap_seg_osc = val[0];
READ(3);
p->tap_idx[0] = val[0];
p->tap_idx[1] = val[1];
p->tap_idx[2] = val[2];
READ(2);
p->osc_min[0] = val[0];
p->osc_min[1] = val[1];
READ(2);
p->osc_max[0] = val[0];
p->osc_max[1] = val[1];
READ(2);
p->w_ofs[0] = val[0];
p->w_ofs[1] = val[1];
READ(2);
p->bright_bl_off_fixed[0] = val[0];
p->bright_bl_off_fixed[1] = val[1];
READ(2);
p->bright_bl_on_min[0] = val[0];
p->bright_bl_on_min[1] = val[1];
READ(2);
p->bright_bl_on_max[0] = val[0];
p->bright_bl_on_max[1] = val[1];
READ(3);
p->battery_threshold[0] = val[0];
p->battery_threshold[1] = val[1];
p->battery_threshold[2] = val[2];
READ(4);
p->s0_idx[0][0] = val[0];
p->s0_idx[0][1] = val[1];
p->s0_idx[0][2] = val[2];
p->s0_idx[0][3] = val[3];
READ(4);
p->s0_idx[1][0] = val[0];
p->s0_idx[1][1] = val[1];
p->s0_idx[1][2] = val[2];
p->s0_idx[1][3] = val[3];
READ(4);
p->s3_idx[0][0] = val[0];
p->s3_idx[0][1] = val[1];
p->s3_idx[0][2] = val[2];
p->s3_idx[0][3] = val[3];
READ(4);
p->s3_idx[1][0] = val[0];
p->s3_idx[1][1] = val[1];
p->s3_idx[1][2] = val[2];
p->s3_idx[1][3] = val[3];
for (i = 0; i < ARRAY_SIZE(p->color); i++) {
READ(3);
p->color[i].r = val[0];
p->color[i].g = val[1];
p->color[i].b = val[2];
}
#undef READ
/* Yay */
r = 0;
done:
if (r)
fprintf(stderr, "problem with line %d: wanted %d, got %d\n",
line, want, got);
fclose(fp);
return r;
}
static void lb_show_params_v1(const struct lightbar_params_v1 *p)
{
int i;
printf("%d\t\t# .google_ramp_up\n", p->google_ramp_up);
printf("%d\t\t# .google_ramp_down\n", p->google_ramp_down);
printf("%d\t\t# .s3s0_ramp_up\n", p->s3s0_ramp_up);
printf("%d\t\t# .s0_tick_delay (battery)\n", p->s0_tick_delay[0]);
printf("%d\t\t# .s0_tick_delay (AC)\n", p->s0_tick_delay[1]);
printf("%d\t\t# .s0a_tick_delay (battery)\n", p->s0a_tick_delay[0]);
printf("%d\t\t# .s0a_tick_delay (AC)\n", p->s0a_tick_delay[1]);
printf("%d\t\t# .s0s3_ramp_down\n", p->s0s3_ramp_down);
printf("%d\t\t# .s3_sleep_for\n", p->s3_sleep_for);
printf("%d\t\t# .s3_ramp_up\n", p->s3_ramp_up);
printf("%d\t\t# .s3_ramp_down\n", p->s3_ramp_down);
printf("%d\t\t# .tap_tick_delay\n", p->tap_tick_delay);
printf("%d\t\t# .tap_gate_delay\n", p->tap_gate_delay);
printf("%d\t\t# .tap_display_time\n", p->tap_display_time);
printf("%d\t\t# .tap_pct_red\n", p->tap_pct_red);
printf("%d\t\t# .tap_pct_green\n", p->tap_pct_green);
printf("%d\t\t# .tap_seg_min_on\n", p->tap_seg_min_on);
printf("%d\t\t# .tap_seg_max_on\n", p->tap_seg_max_on);
printf("%d\t\t# .tap_seg_osc\n", p->tap_seg_osc);
printf("%d %d %d\t\t# .tap_idx\n",
p->tap_idx[0], p->tap_idx[1], p->tap_idx[2]);
printf("0x%02x 0x%02x\t# .osc_min (battery, AC)\n",
p->osc_min[0], p->osc_min[1]);
printf("0x%02x 0x%02x\t# .osc_max (battery, AC)\n",
p->osc_max[0], p->osc_max[1]);
printf("%d %d\t\t# .w_ofs (battery, AC)\n",
p->w_ofs[0], p->w_ofs[1]);
printf("0x%02x 0x%02x\t# .bright_bl_off_fixed (battery, AC)\n",
p->bright_bl_off_fixed[0], p->bright_bl_off_fixed[1]);
printf("0x%02x 0x%02x\t# .bright_bl_on_min (battery, AC)\n",
p->bright_bl_on_min[0], p->bright_bl_on_min[1]);
printf("0x%02x 0x%02x\t# .bright_bl_on_max (battery, AC)\n",
p->bright_bl_on_max[0], p->bright_bl_on_max[1]);
printf("%d %d %d\t# .battery_threshold\n",
p->battery_threshold[0],
p->battery_threshold[1],
p->battery_threshold[2]);
printf("%d %d %d %d\t\t# .s0_idx[] (battery)\n",
p->s0_idx[0][0], p->s0_idx[0][1],
p->s0_idx[0][2], p->s0_idx[0][3]);
printf("%d %d %d %d\t\t# .s0_idx[] (AC)\n",
p->s0_idx[1][0], p->s0_idx[1][1],
p->s0_idx[1][2], p->s0_idx[1][3]);
printf("%d %d %d %d\t# .s3_idx[] (battery)\n",
p->s3_idx[0][0], p->s3_idx[0][1],
p->s3_idx[0][2], p->s3_idx[0][3]);
printf("%d %d %d %d\t# .s3_idx[] (AC)\n",
p->s3_idx[1][0], p->s3_idx[1][1],
p->s3_idx[1][2], p->s3_idx[1][3]);
for (i = 0; i < ARRAY_SIZE(p->color); i++)
printf("0x%02x 0x%02x 0x%02x\t# color[%d]\n",
p->color[i].r,
p->color[i].g,
p->color[i].b, i);
}
static int lb_rd_timing_v2par_from_file(const char *filename,
struct lightbar_params_v2_timing *p)
{
FILE *fp;
char buf[80];
int val[4];
int r = 1;
int line = 0;
int want, got;
fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "Can't open %s: %s\n",
filename, strerror(errno));
return 1;
}
/* We must read the correct number of params from each line */
#define READ(N) do { \
line++; \
want = (N); \
got = -1; \
if (!fgets(buf, sizeof(buf), fp)) \
goto done; \
got = sscanf(buf, "%i %i %i %i", \
&val[0], &val[1], &val[2], &val[3]); \
if (want != got) \
goto done; \
} while (0)
READ(1); p->google_ramp_up = val[0];
READ(1); p->google_ramp_down = val[0];
READ(1); p->s3s0_ramp_up = val[0];
READ(1); p->s0_tick_delay[0] = val[0];
READ(1); p->s0_tick_delay[1] = val[0];
READ(1); p->s0a_tick_delay[0] = val[0];
READ(1); p->s0a_tick_delay[1] = val[0];
READ(1); p->s0s3_ramp_down = val[0];
READ(1); p->s3_sleep_for = val[0];
READ(1); p->s3_ramp_up = val[0];
READ(1); p->s3_ramp_down = val[0];
READ(1); p->tap_tick_delay = val[0];
READ(1); p->tap_gate_delay = val[0];
READ(1); p->tap_display_time = val[0];
#undef READ
/* Yay */
r = 0;
done:
if (r)
fprintf(stderr, "problem with line %d: wanted %d, got %d\n",
line, want, got);
fclose(fp);
return r;
}
static int lb_rd_tap_v2par_from_file(const char *filename,
struct lightbar_params_v2_tap *p)
{
FILE *fp;
char buf[80];
int val[4];
int r = 1;
int line = 0;
int want, got;
fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "Can't open %s: %s\n",
filename, strerror(errno));
return 1;
}
/* We must read the correct number of params from each line */
#define READ(N) do { \
line++; \
want = (N); \
got = -1; \
if (!fgets(buf, sizeof(buf), fp)) \
goto done; \
got = sscanf(buf, "%i %i %i %i", \
&val[0], &val[1], &val[2], &val[3]); \
if (want != got) \
goto done; \
} while (0)
READ(1); p->tap_pct_red = val[0];
READ(1); p->tap_pct_green = val[0];
READ(1); p->tap_seg_min_on = val[0];
READ(1); p->tap_seg_max_on = val[0];
READ(1); p->tap_seg_osc = val[0];
READ(3);
p->tap_idx[0] = val[0];
p->tap_idx[1] = val[1];
p->tap_idx[2] = val[2];
#undef READ
/* Yay */
r = 0;
done:
if (r)
fprintf(stderr, "problem with line %d: wanted %d, got %d\n",
line, want, got);
fclose(fp);
return r;
}
static int lb_rd_osc_v2par_from_file(const char *filename,
struct lightbar_params_v2_oscillation *p)
{
FILE *fp;
char buf[80];
int val[4];
int r = 1;
int line = 0;
int want, got;
fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "Can't open %s: %s\n",
filename, strerror(errno));
return 1;
}
/* We must read the correct number of params from each line */
#define READ(N) do { \
line++; \
want = (N); \
got = -1; \
if (!fgets(buf, sizeof(buf), fp)) \
goto done; \
got = sscanf(buf, "%i %i %i %i", \
&val[0], &val[1], &val[2], &val[3]); \
if (want != got) \
goto done; \
} while (0)
READ(2);
p->osc_min[0] = val[0];
p->osc_min[1] = val[1];
READ(2);
p->osc_max[0] = val[0];
p->osc_max[1] = val[1];
READ(2);
p->w_ofs[0] = val[0];
p->w_ofs[1] = val[1];
#undef READ
/* Yay */
r = 0;
done:
if (r)
fprintf(stderr, "problem with line %d: wanted %d, got %d\n",
line, want, got);
fclose(fp);
return r;
}
static int lb_rd_bright_v2par_from_file(const char *filename,
struct lightbar_params_v2_brightness *p)
{
FILE *fp;
char buf[80];
int val[4];
int r = 1;
int line = 0;
int want, got;
fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "Can't open %s: %s\n",
filename, strerror(errno));
return 1;
}
/* We must read the correct number of params from each line */
#define READ(N) do { \
line++; \
want = (N); \
got = -1; \
if (!fgets(buf, sizeof(buf), fp)) \
goto done; \
got = sscanf(buf, "%i %i %i %i", \
&val[0], &val[1], &val[2], &val[3]); \
if (want != got) \
goto done; \
} while (0)
READ(2);
p->bright_bl_off_fixed[0] = val[0];
p->bright_bl_off_fixed[1] = val[1];
READ(2);
p->bright_bl_on_min[0] = val[0];
p->bright_bl_on_min[1] = val[1];
READ(2);
p->bright_bl_on_max[0] = val[0];
p->bright_bl_on_max[1] = val[1];
#undef READ
/* Yay */
r = 0;
done:
if (r)
fprintf(stderr, "problem with line %d: wanted %d, got %d\n",
line, want, got);
fclose(fp);
return r;
}
static int lb_rd_thlds_v2par_from_file(const char *filename,
struct lightbar_params_v2_thresholds *p)
{
FILE *fp;
char buf[80];
int val[4];
int r = 1;
int line = 0;
int want, got;
fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "Can't open %s: %s\n",
filename, strerror(errno));
return 1;
}
/* We must read the correct number of params from each line */
#define READ(N) do { \
line++; \
want = (N); \
got = -1; \
if (!fgets(buf, sizeof(buf), fp)) \
goto done; \
got = sscanf(buf, "%i %i %i %i", \
&val[0], &val[1], &val[2], &val[3]); \
if (want != got) \
goto done; \
} while (0)
READ(3);
p->battery_threshold[0] = val[0];
p->battery_threshold[1] = val[1];
p->battery_threshold[2] = val[2];
#undef READ
/* Yay */
r = 0;
done:
if (r)
fprintf(stderr, "problem with line %d: wanted %d, got %d\n",
line, want, got);
fclose(fp);
return r;
}
static int lb_rd_colors_v2par_from_file(const char *filename,
struct lightbar_params_v2_colors *p)
{
FILE *fp;
char buf[80];
int val[4];
int r = 1;
int line = 0;
int want, got;
int i;
fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "Can't open %s: %s\n",
filename, strerror(errno));
return 1;
}
/* We must read the correct number of params from each line */
#define READ(N) do { \
line++; \
want = (N); \
got = -1; \
if (!fgets(buf, sizeof(buf), fp)) \
goto done; \
got = sscanf(buf, "%i %i %i %i", \
&val[0], &val[1], &val[2], &val[3]); \
if (want != got) \
goto done; \
} while (0)
READ(4);
p->s0_idx[0][0] = val[0];
p->s0_idx[0][1] = val[1];
p->s0_idx[0][2] = val[2];
p->s0_idx[0][3] = val[3];
READ(4);
p->s0_idx[1][0] = val[0];
p->s0_idx[1][1] = val[1];
p->s0_idx[1][2] = val[2];
p->s0_idx[1][3] = val[3];
READ(4);
p->s3_idx[0][0] = val[0];
p->s3_idx[0][1] = val[1];
p->s3_idx[0][2] = val[2];
p->s3_idx[0][3] = val[3];
READ(4);
p->s3_idx[1][0] = val[0];
p->s3_idx[1][1] = val[1];
p->s3_idx[1][2] = val[2];
p->s3_idx[1][3] = val[3];
for (i = 0; i < ARRAY_SIZE(p->color); i++) {
READ(3);
p->color[i].r = val[0];
p->color[i].g = val[1];
p->color[i].b = val[2];
}
#undef READ
/* Yay */
r = 0;
done:
if (r)
fprintf(stderr, "problem with line %d: wanted %d, got %d\n",
line, want, got);
fclose(fp);
return r;
}
static void lb_show_v2par_timing(const struct lightbar_params_v2_timing *p)
{
printf("%d\t\t# .google_ramp_up\n", p->google_ramp_up);
printf("%d\t\t# .google_ramp_down\n", p->google_ramp_down);
printf("%d\t\t# .s3s0_ramp_up\n", p->s3s0_ramp_up);
printf("%d\t\t# .s0_tick_delay (battery)\n", p->s0_tick_delay[0]);
printf("%d\t\t# .s0_tick_delay (AC)\n", p->s0_tick_delay[1]);
printf("%d\t\t# .s0a_tick_delay (battery)\n", p->s0a_tick_delay[0]);
printf("%d\t\t# .s0a_tick_delay (AC)\n", p->s0a_tick_delay[1]);
printf("%d\t\t# .s0s3_ramp_down\n", p->s0s3_ramp_down);
printf("%d\t\t# .s3_sleep_for\n", p->s3_sleep_for);
printf("%d\t\t# .s3_ramp_up\n", p->s3_ramp_up);
printf("%d\t\t# .s3_ramp_down\n", p->s3_ramp_down);
printf("%d\t\t# .tap_tick_delay\n", p->tap_tick_delay);
printf("%d\t\t# .tap_gate_delay\n", p->tap_gate_delay);
printf("%d\t\t# .tap_display_time\n", p->tap_display_time);
}
static void lb_show_v2par_tap(const struct lightbar_params_v2_tap *p)
{
printf("%d\t\t# .tap_pct_red\n", p->tap_pct_red);
printf("%d\t\t# .tap_pct_green\n", p->tap_pct_green);
printf("%d\t\t# .tap_seg_min_on\n", p->tap_seg_min_on);
printf("%d\t\t# .tap_seg_max_on\n", p->tap_seg_max_on);
printf("%d\t\t# .tap_seg_osc\n", p->tap_seg_osc);
printf("%d %d %d\t\t# .tap_idx\n",
p->tap_idx[0], p->tap_idx[1], p->tap_idx[2]);
}
static void lb_show_v2par_osc(const struct lightbar_params_v2_oscillation *p)
{
printf("0x%02x 0x%02x\t# .osc_min (battery, AC)\n",
p->osc_min[0], p->osc_min[1]);
printf("0x%02x 0x%02x\t# .osc_max (battery, AC)\n",
p->osc_max[0], p->osc_max[1]);
printf("%d %d\t\t# .w_ofs (battery, AC)\n",
p->w_ofs[0], p->w_ofs[1]);
}
static void lb_show_v2par_bright(const struct lightbar_params_v2_brightness *p)
{
printf("0x%02x 0x%02x\t# .bright_bl_off_fixed (battery, AC)\n",
p->bright_bl_off_fixed[0], p->bright_bl_off_fixed[1]);
printf("0x%02x 0x%02x\t# .bright_bl_on_min (battery, AC)\n",
p->bright_bl_on_min[0], p->bright_bl_on_min[1]);
printf("0x%02x 0x%02x\t# .bright_bl_on_max (battery, AC)\n",
p->bright_bl_on_max[0], p->bright_bl_on_max[1]);
}
static void lb_show_v2par_thlds(const struct lightbar_params_v2_thresholds *p)
{
printf("%d %d %d\t# .battery_threshold\n",
p->battery_threshold[0],
p->battery_threshold[1],
p->battery_threshold[2]);
}
static void lb_show_v2par_colors(const struct lightbar_params_v2_colors *p)
{
int i;
printf("%d %d %d %d\t\t# .s0_idx[] (battery)\n",
p->s0_idx[0][0], p->s0_idx[0][1],
p->s0_idx[0][2], p->s0_idx[0][3]);
printf("%d %d %d %d\t\t# .s0_idx[] (AC)\n",
p->s0_idx[1][0], p->s0_idx[1][1],
p->s0_idx[1][2], p->s0_idx[1][3]);
printf("%d %d %d %d\t# .s3_idx[] (battery)\n",
p->s3_idx[0][0], p->s3_idx[0][1],
p->s3_idx[0][2], p->s3_idx[0][3]);
printf("%d %d %d %d\t# .s3_idx[] (AC)\n",
p->s3_idx[1][0], p->s3_idx[1][1],
p->s3_idx[1][2], p->s3_idx[1][3]);
for (i = 0; i < ARRAY_SIZE(p->color); i++)
printf("0x%02x 0x%02x 0x%02x\t# color[%d]\n",
p->color[i].r,
p->color[i].g,
p->color[i].b, i);
}
static int lb_load_program(const char *filename, struct lightbar_program *prog)
{
FILE *fp;
size_t got;
int rc;
fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "Can't open %s: %s\n",
filename, strerror(errno));
return 1;
}
rc = fseek(fp, 0, SEEK_END);
if (rc) {
fprintf(stderr, "Couldn't find end of file %s",
filename);
fclose(fp);
return 1;
}
rc = (int) ftell(fp);
if (rc > EC_LB_PROG_LEN) {
fprintf(stderr, "File %s is too long, aborting\n", filename);
fclose(fp);
return 1;
}
rewind(fp);
memset(prog->data, 0, EC_LB_PROG_LEN);
got = fread(prog->data, 1, EC_LB_PROG_LEN, fp);
if (rc != got)
fprintf(stderr, "Warning: did not read entire file\n");
prog->size = got;
fclose(fp);
return 0;
}
static int cmd_lightbar_params_v0(int argc, char **argv)
{
struct ec_params_lightbar param;
struct ec_response_lightbar resp;
int r;
if (argc > 2) {
r = lb_read_params_v0_from_file(argv[2],
¶m.set_params_v0);
if (r)
return r;
return lb_do_cmd(LIGHTBAR_CMD_SET_PARAMS_V0,
¶m, &resp);
}
r = lb_do_cmd(LIGHTBAR_CMD_GET_PARAMS_V0, ¶m, &resp);
if (!r)
lb_show_params_v0(&resp.get_params_v0);
return r;
}
static int cmd_lightbar_params_v1(int argc, char **argv)
{
struct ec_params_lightbar param;
struct ec_response_lightbar resp;
int r;
if (argc > 2) {
r = lb_read_params_v1_from_file(argv[2],
¶m.set_params_v1);
if (r)
return r;
return lb_do_cmd(LIGHTBAR_CMD_SET_PARAMS_V1,
¶m, &resp);
}
r = lb_do_cmd(LIGHTBAR_CMD_GET_PARAMS_V1, ¶m, &resp);
if (!r)
lb_show_params_v1(&resp.get_params_v1);
return r;
}
static void lb_param_v2_help(void)
{
printf("Usage:\n");
printf("lightbar params2 group [setfile]\n");
printf("group list:\n");
printf(" timing\n");
printf(" tap\n");
printf(" oscillation\n");
printf(" brightness\n");
printf(" thresholds\n");
printf(" colors\n");
return;
}
static int cmd_lightbar_params_v2(int argc, char **argv)
{
struct ec_params_lightbar p;
struct ec_response_lightbar resp;
int r = 0;
int set = 0;
memset(&p, 0, sizeof(struct ec_params_lightbar));
memset(&resp, 0, sizeof(struct ec_response_lightbar));
if (argc < 3) {
lb_param_v2_help();
return 1;
}
/* Set new params if provided with a setfile */
if (argc > 3)
set = 1;
/* Show selected v2 params */
if (!strncasecmp(argv[2], "timing", 6)) {
if (set) {
r = lb_rd_timing_v2par_from_file(argv[3],
&p.set_v2par_timing);
if (r)
return r;
r = lb_do_cmd(LIGHTBAR_CMD_SET_PARAMS_V2_TIMING,
&p, &resp);
if (r)
return r;
}
r = lb_do_cmd(LIGHTBAR_CMD_GET_PARAMS_V2_TIMING, &p, &resp);
if (r)
return r;
lb_show_v2par_timing(&resp.get_params_v2_timing);
} else if (!strcasecmp(argv[2], "tap")) {
if (set) {
r = lb_rd_tap_v2par_from_file(argv[3],
&p.set_v2par_tap);
if (r)
return r;
r = lb_do_cmd(LIGHTBAR_CMD_SET_PARAMS_V2_TAP,
&p, &resp);
if (r)
return r;
}
r = lb_do_cmd(LIGHTBAR_CMD_GET_PARAMS_V2_TAP, &p, &resp);
if (r)
return r;
lb_show_v2par_tap(&resp.get_params_v2_tap);
} else if (!strncasecmp(argv[2], "oscillation", 11)) {
if (set) {
r = lb_rd_osc_v2par_from_file(argv[3],
&p.set_v2par_osc);
if (r)
return r;
r = lb_do_cmd(LIGHTBAR_CMD_SET_PARAMS_V2_OSCILLATION,
&p, &resp);
if (r)
return r;
}
r = lb_do_cmd(LIGHTBAR_CMD_GET_PARAMS_V2_OSCILLATION, &p,
&resp);
if (r)
return r;
lb_show_v2par_osc(&resp.get_params_v2_osc);
} else if (!strncasecmp(argv[2], "brightness", 10)) {
if (set) {
r = lb_rd_bright_v2par_from_file(argv[3],
&p.set_v2par_bright);
if (r)
return r;
r = lb_do_cmd(LIGHTBAR_CMD_SET_PARAMS_V2_BRIGHTNESS,
&p, &resp);
if (r)
return r;
}
r = lb_do_cmd(LIGHTBAR_CMD_GET_PARAMS_V2_BRIGHTNESS, &p,
&resp);
if (r)
return r;
lb_show_v2par_bright(&resp.get_params_v2_bright);
} else if (!strncasecmp(argv[2], "thresholds", 10)) {
if (set) {
r = lb_rd_thlds_v2par_from_file(argv[3],
&p.set_v2par_thlds);
if (r)
return r;
r = lb_do_cmd(LIGHTBAR_CMD_SET_PARAMS_V2_THRESHOLDS,
&p, &resp);
if (r)
return r;
}
r = lb_do_cmd(LIGHTBAR_CMD_GET_PARAMS_V2_THRESHOLDS, &p,
&resp);
if (r)
return r;
lb_show_v2par_thlds(&resp.get_params_v2_thlds);
} else if (!strncasecmp(argv[2], "colors", 6)) {
if (set) {
r = lb_rd_colors_v2par_from_file(argv[3],
&p.set_v2par_colors);
if (r)
return r;
r = lb_do_cmd(LIGHTBAR_CMD_SET_PARAMS_V2_COLORS,
&p, &resp);
if (r)
return r;
}
r = lb_do_cmd(LIGHTBAR_CMD_GET_PARAMS_V2_COLORS, &p, &resp);
if (r)
return r;
lb_show_v2par_colors(&resp.get_params_v2_colors);
} else {
lb_param_v2_help();
}
return r;
}
static int cmd_lightbar(int argc, char **argv)
{
int i, r;
struct ec_params_lightbar param;
struct ec_response_lightbar resp;
if (1 == argc) { /* no args = dump 'em all */
r = lb_do_cmd(LIGHTBAR_CMD_DUMP, ¶m, &resp);
if (r)
return r;
for (i = 0; i < ARRAY_SIZE(resp.dump.vals); i++) {
printf(" %02x %02x %02x\n",
resp.dump.vals[i].reg,
resp.dump.vals[i].ic0,
resp.dump.vals[i].ic1);
}
return 0;
}
if (argc == 2 && !strcasecmp(argv[1], "init"))
return lb_do_cmd(LIGHTBAR_CMD_INIT, ¶m, &resp);
if (argc == 2 && !strcasecmp(argv[1], "off"))
return lb_do_cmd(LIGHTBAR_CMD_OFF, ¶m, &resp);
if (argc == 2 && !strcasecmp(argv[1], "on"))
return lb_do_cmd(LIGHTBAR_CMD_ON, ¶m, &resp);
if (!strcasecmp(argv[1], "params0"))
return cmd_lightbar_params_v0(argc, argv);
if (!strcasecmp(argv[1], "params1"))
return cmd_lightbar_params_v1(argc, argv);
if (!strcasecmp(argv[1], "params2"))
return cmd_lightbar_params_v2(argc, argv);
if (!strcasecmp(argv[1], "params")) {
/* Just try them both */
fprintf(stderr, "trying params1 ...\n");
if (0 == cmd_lightbar_params_v1(argc, argv))
return 0;
fprintf(stderr, "trying params0 ...\n");
return cmd_lightbar_params_v0(argc, argv);
}
if (!strcasecmp(argv[1], "version")) {
r = lb_do_cmd(LIGHTBAR_CMD_VERSION, ¶m, &resp);
if (!r)
printf("version %d flags 0x%x\n",
resp.version.num, resp.version.flags);
return r;
}
if (argc > 1 && !strcasecmp(argv[1], "brightness")) {
char *e;
int rv;
if (argc > 2) {
param.set_brightness.num = 0xff &
strtoul(argv[2], &e, 16);
return lb_do_cmd(LIGHTBAR_CMD_SET_BRIGHTNESS,
¶m, &resp);
}
rv = lb_do_cmd(LIGHTBAR_CMD_GET_BRIGHTNESS,
¶m, &resp);
if (rv)
return rv;
printf("%02x\n", resp.get_brightness.num);
return 0;
}
if (argc > 1 && !strcasecmp(argv[1], "demo")) {
int rv;
if (argc > 2) {
if (!strcasecmp(argv[2], "on") || argv[2][0] == '1')
param.demo.num = 1;
else if (!strcasecmp(argv[2], "off") ||
argv[2][0] == '0')
param.demo.num = 0;
else {
fprintf(stderr, "Invalid arg\n");
return -1;
}
return lb_do_cmd(LIGHTBAR_CMD_DEMO, ¶m, &resp);
}
rv = lb_do_cmd(LIGHTBAR_CMD_GET_DEMO, ¶m, &resp);
if (rv)
return rv;
printf("%s\n", resp.get_demo.num ? "on" : "off");
return 0;
}
if (argc >= 2 && !strcasecmp(argv[1], "seq")) {
char *e;
uint8_t num;
if (argc == 2)
return lb_show_msg_names();
num = 0xff & strtoul(argv[2], &e, 16);
if (e && *e)
num = lb_find_msg_by_name(argv[2]);
if (num >= LIGHTBAR_NUM_SEQUENCES) {
fprintf(stderr, "Invalid arg\n");
return -1;
}
param.seq.num = num;
return lb_do_cmd(LIGHTBAR_CMD_SEQ, ¶m, &resp);
}
if (argc >= 3 && !strcasecmp(argv[1], "program")) {
lb_load_program(argv[2], ¶m.set_program);
return lb_do_cmd(LIGHTBAR_CMD_SET_PROGRAM, ¶m, &resp);
}
if (argc == 4) {
char *e;
param.reg.ctrl = 0xff & strtoul(argv[1], &e, 16);
param.reg.reg = 0xff & strtoul(argv[2], &e, 16);
param.reg.value = 0xff & strtoul(argv[3], &e, 16);
return lb_do_cmd(LIGHTBAR_CMD_REG, ¶m, &resp);
}
if (argc == 5) {
char *e;
param.set_rgb.led = strtoul(argv[1], &e, 16);
param.set_rgb.red = strtoul(argv[2], &e, 16);
param.set_rgb.green = strtoul(argv[3], &e, 16);
param.set_rgb.blue = strtoul(argv[4], &e, 16);
return lb_do_cmd(LIGHTBAR_CMD_SET_RGB, ¶m, &resp);
}
/* Only thing left is to try to read an LED value */
if (argc == 2) {
char *e;
param.get_rgb.led = strtoul(argv[1], &e, 0);
if (!(e && *e)) {
r = lb_do_cmd(LIGHTBAR_CMD_GET_RGB, ¶m, &resp);
if (r)
return r;
printf("%02x %02x %02x\n",
resp.get_rgb.red,
resp.get_rgb.green,
resp.get_rgb.blue);
return 0;
}
}
return lb_help(argv[0]);
}
/* Create an array to store sizes of motion sense param and response structs. */
#define ST_CMD_SIZE ST_FLD_SIZE(ec_params_motion_sense, cmd)
#define ST_PRM_SIZE(SUBCMD) \
(ST_CMD_SIZE + ST_FLD_SIZE(ec_params_motion_sense, SUBCMD))
#define ST_RSP_SIZE(SUBCMD) ST_FLD_SIZE(ec_response_motion_sense, SUBCMD)
#define ST_BOTH_SIZES(SUBCMD) { ST_PRM_SIZE(SUBCMD), ST_RSP_SIZE(SUBCMD) }
/*
* For ectool only, assume no more than 16 sensors. More advanced
* implementation would allocate the right amount of memory depending on the
* number of sensors.
*/
#define ECTOOL_MAX_SENSOR 16
static const struct {
uint8_t outsize;
uint8_t insize;
} ms_command_sizes[] = {
{
ST_PRM_SIZE(dump),
ST_RSP_SIZE(dump) +
sizeof(struct ec_response_motion_sensor_data) *
ECTOOL_MAX_SENSOR
},
ST_BOTH_SIZES(info_3),
ST_BOTH_SIZES(ec_rate),
ST_BOTH_SIZES(sensor_odr),
ST_BOTH_SIZES(sensor_range),
ST_BOTH_SIZES(kb_wake_angle),
ST_BOTH_SIZES(data),
{
ST_CMD_SIZE,
ST_RSP_SIZE(fifo_info) + sizeof(uint16_t) * ECTOOL_MAX_SENSOR
},
ST_BOTH_SIZES(fifo_flush),
ST_BOTH_SIZES(fifo_read),
ST_BOTH_SIZES(perform_calib),
ST_BOTH_SIZES(sensor_offset),
ST_BOTH_SIZES(list_activities),
{ ST_PRM_SIZE(set_activity), 0 },
{ ST_CMD_SIZE, ST_RSP_SIZE(lid_angle) },
ST_BOTH_SIZES(fifo_int_enable),
ST_BOTH_SIZES(spoof),
ST_BOTH_SIZES(tablet_mode_threshold),
ST_BOTH_SIZES(sensor_scale),
};
BUILD_ASSERT(ARRAY_SIZE(ms_command_sizes) == MOTIONSENSE_NUM_CMDS);
#undef ST_CMD_SIZE
#undef ST_PRM_SIZE
#undef ST_RSP_SIZE
#undef ST_BOTH_SIZES
static int ms_help(const char *cmd)
{
printf("Usage:\n");
printf(" %s - dump all motion data\n", cmd);
printf(" %s active - print active flag\n", cmd);
printf(" %s info NUM - print sensor info\n", cmd);
printf(" %s ec_rate [RATE_MS] - set/get sample rate\n", cmd);
printf(" %s odr NUM [ODR [ROUNDUP]] - set/get sensor ODR\n", cmd);
printf(" %s range NUM [RANGE [ROUNDUP]]- set/get sensor range\n", cmd);
printf(" %s offset NUM - get sensor offset\n", cmd);
printf(" %s kb_wake NUM - set/get KB wake ang\n", cmd);
printf(" %s data NUM - read sensor latest data\n",
cmd);
printf(" %s fifo_info - print fifo info\n", cmd);
printf(" %s fifo_int_enable [0/1] - enable/disable/get fifo "
"interrupt status\n", cmd);
printf(" %s fifo_read MAX_DATA - read fifo data\n", cmd);
printf(" %s fifo_flush NUM - trigger fifo interrupt\n",
cmd);
printf(" %s list_activities NUM - list supported activities\n",
cmd);
printf(" %s set_activity NUM ACT EN - enable/disable activity\n",
cmd);
printf(" %s lid_angle - print lid angle\n", cmd);
printf(" %s spoof -- NUM [0/1] [X Y Z] - enable/disable spoofing\n",
cmd);
printf(" %s tablet_mode_angle ANG HYS - set/get tablet mode angle\n",
cmd);
return 0;
}
static void motionsense_display_activities(uint32_t activities)
{
if (activities & BIT(MOTIONSENSE_ACTIVITY_SIG_MOTION))
printf("%d: Significant motion\n",
MOTIONSENSE_ACTIVITY_SIG_MOTION);
if (activities & BIT(MOTIONSENSE_ACTIVITY_DOUBLE_TAP))
printf("%d: Double tap\n",
MOTIONSENSE_ACTIVITY_DOUBLE_TAP);
}
static int cmd_motionsense(int argc, char **argv)
{
int i, rv, status_only = (argc == 2);
struct ec_params_motion_sense param;
/* The largest size using resp as a response buffer */
uint8_t resp_buffer[ms_command_sizes[MOTIONSENSE_CMD_DUMP].insize];
struct ec_response_motion_sense *resp =
(struct ec_response_motion_sense *)resp_buffer;
char *e;
/*
* Warning: the following strings printed out are read in an
* autotest. Do not change string without consulting autotest
* for kernel_CrosECSysfsAccel.
*/
const char *motion_status_string[2][2] = {
{ "Motion sensing inactive", "0"},
{ "Motion sensing active", "1"},
};
/* No motionsense command has more than 7 args. */
if (argc > 7)
return ms_help(argv[0]);
if ((argc == 1) ||
(argc == 2 && !strcasecmp(argv[1], "active"))) {
param.cmd = MOTIONSENSE_CMD_DUMP;
param.dump.max_sensor_count = ECTOOL_MAX_SENSOR;
rv = ec_command(
EC_CMD_MOTION_SENSE_CMD, 1,
¶m, ms_command_sizes[param.cmd].outsize,
resp, ms_command_sizes[param.cmd].insize);
if (rv > 0) {
printf("%s\n", motion_status_string[
!!(resp->dump.module_flags &
MOTIONSENSE_MODULE_FLAG_ACTIVE)][
status_only]);
if (status_only)
return 0;
if (resp->dump.sensor_count > ECTOOL_MAX_SENSOR) {
printf("Too many sensors to handle: %d",
resp->dump.sensor_count);
return -1;
}
for (i = 0; i < resp->dump.sensor_count; i++) {
/*
* Warning: the following string printed out
* is read by an autotest. Do not change string
* without consulting autotest for
* kernel_CrosECSysfsAccel.
*/
printf("Sensor %d: ", i);
if (resp->dump.sensor[i].flags &
MOTIONSENSE_SENSOR_FLAG_PRESENT)
printf("%d\t%d\t%d\n",
resp->dump.sensor[i].data[0],
resp->dump.sensor[i].data[1],
resp->dump.sensor[i].data[2]);
else
printf("None\n");
}
return 0;
} else {
return rv;
}
}
if (argc == 3 && !strcasecmp(argv[1], "info")) {
int version = 0;
rv = get_latest_cmd_version(EC_CMD_MOTION_SENSE_CMD, &version);
if (rv < 0)
return rv;
param.cmd = MOTIONSENSE_CMD_INFO;
param.sensor_odr.sensor_num = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad %s arg.\n", argv[2]);
return -1;
}
rv = ec_command(EC_CMD_MOTION_SENSE_CMD, version,
¶m, ms_command_sizes[param.cmd].outsize,
resp, ms_command_sizes[param.cmd].insize);
if (rv < 0)
return rv;
printf("Type: ");
switch (resp->info.type) {
case MOTIONSENSE_TYPE_ACCEL:
printf("accel\n");
break;
case MOTIONSENSE_TYPE_GYRO:
printf("gyro\n");
break;
case MOTIONSENSE_TYPE_MAG:
printf("magnetometer\n");
break;
case MOTIONSENSE_TYPE_LIGHT:
printf("light\n");
break;
case MOTIONSENSE_TYPE_PROX:
printf("proximity\n");
break;
case MOTIONSENSE_TYPE_ACTIVITY:
printf("activity\n");
break;
case MOTIONSENSE_TYPE_BARO:
printf("barometer\n");
break;
case MOTIONSENSE_TYPE_SYNC:
printf("sync\n");
break;
default:
printf("unknown\n");
}
printf("Location: ");
switch (resp->info.location) {
case MOTIONSENSE_LOC_BASE:
printf("base\n");
break;
case MOTIONSENSE_LOC_LID:
printf("lid\n");
break;
case MOTIONSENSE_LOC_CAMERA:
printf("camera\n");
break;
default:
printf("unknown\n");
}
printf("Chip: ");
switch (resp->info.chip) {
case MOTIONSENSE_CHIP_KXCJ9:
printf("kxcj9\n");
break;
case MOTIONSENSE_CHIP_LSM6DS0:
printf("lsm6ds0\n");
break;
case MOTIONSENSE_CHIP_BMI160:
printf("bmi160\n");
break;
case MOTIONSENSE_CHIP_SI1141:
printf("si1141\n");
break;
case MOTIONSENSE_CHIP_KX022:
printf("kx022\n");
break;
case MOTIONSENSE_CHIP_L3GD20H:
printf("l3gd20h\n");
break;
case MOTIONSENSE_CHIP_BMA255:
printf("bma255\n");
break;
case MOTIONSENSE_CHIP_BMP280:
printf("bmp280\n");
break;
case MOTIONSENSE_CHIP_OPT3001:
printf("opt3001\n");
break;
case MOTIONSENSE_CHIP_BH1730:
printf("bh1730\n");
break;
case MOTIONSENSE_CHIP_GPIO:
printf("gpio\n");
break;
case MOTIONSENSE_CHIP_LIS2DH:
printf("lis2dh\n");
break;
case MOTIONSENSE_CHIP_LSM6DSM:
printf("lsm6dsm\n");
break;
case MOTIONSENSE_CHIP_LIS2DE:
printf("lis2de\n");
break;
case MOTIONSENSE_CHIP_LIS2MDL:
printf("lis2mdl\n");
break;
case MOTIONSENSE_CHIP_LSM6DS3:
printf("lsm6ds3\n");
break;
case MOTIONSENSE_CHIP_LSM6DSO:
printf("lsm6dso\n");
break;
case MOTIONSENSE_CHIP_LNG2DM:
printf("lng2dm\n");
break;
default:
printf("unknown\n");
}
if (version >= 3) {
printf("Min Frequency: %d mHz\n",
resp->info_3.min_frequency);
printf("Max Frequency: %d mHz\n",
resp->info_3.max_frequency);
printf("FIFO Max Event Count: %d\n",
resp->info_3.fifo_max_event_count);
}
return 0;
}
if (argc < 4 && !strcasecmp(argv[1], "ec_rate")) {
param.cmd = MOTIONSENSE_CMD_EC_RATE;
param.ec_rate.data = EC_MOTION_SENSE_NO_VALUE;
if (argc == 3) {
param.ec_rate.data = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad %s arg.\n", argv[2]);
return -1;
}
}
rv = ec_command(EC_CMD_MOTION_SENSE_CMD, 1,
¶m, ms_command_sizes[param.cmd].outsize,
resp, ms_command_sizes[param.cmd].insize);
if (rv < 0)
return rv;
printf("%d\n", resp->ec_rate.ret);
return 0;
}
if (argc > 2 && !strcasecmp(argv[1], "odr")) {
param.cmd = MOTIONSENSE_CMD_SENSOR_ODR;
param.sensor_odr.data = EC_MOTION_SENSE_NO_VALUE;
param.sensor_odr.roundup = 1;
param.sensor_odr.sensor_num = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad %s arg.\n", argv[2]);
return -1;
}
if (argc >= 4) {
param.sensor_odr.data = strtol(argv[3], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad %s arg.\n", argv[3]);
return -1;
}
}
if (argc == 5) {
param.sensor_odr.roundup = strtol(argv[4], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad %s arg.\n", argv[4]);
return -1;
}
}
rv = ec_command(EC_CMD_MOTION_SENSE_CMD, 1,
¶m, ms_command_sizes[param.cmd].outsize,
resp, ms_command_sizes[param.cmd].insize);
if (rv < 0)
return rv;
printf("%d\n", resp->sensor_odr.ret);
return 0;
}
if (argc > 2 && !strcasecmp(argv[1], "range")) {
param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE;
param.sensor_range.data = EC_MOTION_SENSE_NO_VALUE;
param.sensor_range.roundup = 1;
param.sensor_range.sensor_num = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad %s arg.\n", argv[2]);
return -1;
}
if (argc >= 4) {
param.sensor_range.data = strtol(argv[3], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad %s arg.\n", argv[3]);
return -1;
}
}
if (argc == 5) {
param.sensor_odr.roundup = strtol(argv[4], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad %s arg.\n", argv[4]);
return -1;
}
}
rv = ec_command(EC_CMD_MOTION_SENSE_CMD, 1,
¶m, ms_command_sizes[param.cmd].outsize,
resp, ms_command_sizes[param.cmd].insize);
if (rv < 0)
return rv;
printf("%d\n", resp->sensor_range.ret);
return 0;
}
if (argc < 4 && !strcasecmp(argv[1], "kb_wake")) {
param.cmd = MOTIONSENSE_CMD_KB_WAKE_ANGLE;
param.kb_wake_angle.data = EC_MOTION_SENSE_NO_VALUE;
if (argc == 3) {
param.kb_wake_angle.data = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad %s arg.\n", argv[2]);
return -1;
}
}
rv = ec_command(EC_CMD_MOTION_SENSE_CMD, 1,
¶m, ms_command_sizes[param.cmd].outsize,
resp, ms_command_sizes[param.cmd].insize);
if (rv < 0)
return rv;
printf("%d\n", resp->kb_wake_angle.ret);
return 0;
}
if (argc < 5 && !strcasecmp(argv[1], "tablet_mode_angle")) {
param.cmd = MOTIONSENSE_CMD_TABLET_MODE_LID_ANGLE;
/*
* EC_MOTION_SENSE_NO_VALUE indicates to the EC that host is
* attempting to only read the current values.
*/
param.tablet_mode_threshold.lid_angle =
EC_MOTION_SENSE_NO_VALUE;
param.tablet_mode_threshold.hys_degree =
EC_MOTION_SENSE_NO_VALUE;
if (argc == 4) {
param.tablet_mode_threshold.lid_angle = strtol(argv[2],
&e, 0);
if (e && *e) {
fprintf(stderr, "Bad %s arg.\n", argv[2]);
return -1;
}
param.tablet_mode_threshold.hys_degree = strtol(argv[3],
&e, 0);
if (e && *e) {
fprintf(stderr, "Bad %s arg.\n", argv[3]);
return -1;
}
} else if (argc != 2) {
return ms_help(argv[0]);
}
rv = ec_command(EC_CMD_MOTION_SENSE_CMD, 2,
¶m, ms_command_sizes[param.cmd].outsize,
resp, ms_command_sizes[param.cmd].insize);
if (rv < 0)
return rv;
printf("tablet_mode_angle=%d hys=%d\n",
resp->tablet_mode_threshold.lid_angle,
resp->tablet_mode_threshold.hys_degree);
return 0;
}
if (argc == 2 && !strcasecmp(argv[1], "fifo_info")) {
int sensor_count;
param.cmd = MOTIONSENSE_CMD_DUMP;
param.dump.max_sensor_count = 0;
rv = ec_command(EC_CMD_MOTION_SENSE_CMD, 1,
¶m, ms_command_sizes[param.cmd].outsize,
resp, ms_command_sizes[param.cmd].insize);
if (rv < 0)
return rv;
sensor_count = resp->dump.sensor_count;
param.cmd = MOTIONSENSE_CMD_FIFO_INFO;
rv = ec_command(EC_CMD_MOTION_SENSE_CMD, 2,
¶m, ms_command_sizes[param.cmd].outsize,
resp, ms_command_sizes[param.cmd].insize);
if (rv < 0)
return rv;
printf("Size: %d\n", resp->fifo_info.size);
printf("Count: %d\n", resp->fifo_info.count);
printf("Timestamp:%" PRIx32 "\n", resp->fifo_info.timestamp);
printf("Total lost: %d\n", resp->fifo_info.total_lost);
for (i = 0; i < sensor_count; i++) {
int lost = resp->fifo_info.lost[i];
if (lost != 0)
printf("Lost %d: %d\n", i, lost);
}
return 0;
}
if (argc >= 2 && !strcasecmp(argv[1], "fifo_int_enable")) {
param.cmd = MOTIONSENSE_CMD_FIFO_INT_ENABLE;
if (argc == 3)
param.fifo_int_enable.enable = strtol(argv[2], &e, 0);
else
param.fifo_int_enable.enable = EC_MOTION_SENSE_NO_VALUE;
rv = ec_command(EC_CMD_MOTION_SENSE_CMD, 2,
¶m, ms_command_sizes[param.cmd].outsize,
resp, ms_command_sizes[param.cmd].insize);
if (rv < 0)
return rv;
printf("%d\n", resp->fifo_int_enable.ret);
return 0;
}
if (argc == 3 && !strcasecmp(argv[1], "fifo_read")) {
/* large number to test fragmentation */
struct {
uint32_t number_data;
struct ec_response_motion_sensor_data data[512];
} fifo_read_buffer = {
.number_data = -1,
};
int print_data = 0, max_data = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad %s arg.\n", argv[2]);
return -1;
}
while (fifo_read_buffer.number_data != 0 &&
print_data < max_data) {
struct ec_response_motion_sensor_data *vector;
param.cmd = MOTIONSENSE_CMD_FIFO_READ;
param.fifo_read.max_data_vector =
MIN(ARRAY_SIZE(fifo_read_buffer.data),
max_data - print_data);
rv = ec_command(EC_CMD_MOTION_SENSE_CMD, 2,
¶m,
ms_command_sizes[param.cmd].outsize,
&fifo_read_buffer, ec_max_insize);
if (rv < 0)
return rv;
print_data += fifo_read_buffer.number_data;
for (i = 0; i < fifo_read_buffer.number_data; i++) {
vector = &fifo_read_buffer.data[i];
if (vector->flags &
(MOTIONSENSE_SENSOR_FLAG_TIMESTAMP |
MOTIONSENSE_SENSOR_FLAG_FLUSH)) {
uint32_t timestamp = 0;
memcpy(×tamp, vector->data,
sizeof(uint32_t));
printf("Timestamp:%" PRIx32 "%s\n",
timestamp,
(vector->flags &
MOTIONSENSE_SENSOR_FLAG_FLUSH ?
" - Flush" : ""));
} else {
printf("Sensor %d: %d\t%d\t%d\n",
vector->sensor_num,
vector->data[0],
vector->data[1],
vector->data[2]);
}
}
}
return 0;
}
if (argc == 3 && !strcasecmp(argv[1], "fifo_flush")) {
param.cmd = MOTIONSENSE_CMD_FIFO_FLUSH;
param.sensor_odr.sensor_num = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad %s arg.\n", argv[2]);
return -1;
}
rv = ec_command(EC_CMD_MOTION_SENSE_CMD, 1,
¶m, ms_command_sizes[param.cmd].outsize,
resp, ms_command_sizes[param.cmd].insize);
return rv < 0 ? rv : 0;
}
if (argc == 3 && !strcasecmp(argv[1], "offset")) {
param.cmd = MOTIONSENSE_CMD_SENSOR_OFFSET;
param.sensor_offset.flags = 0;
param.sensor_offset.sensor_num = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad %s arg.\n", argv[2]);
return -1;
}
rv = ec_command(EC_CMD_MOTION_SENSE_CMD, 1,
¶m, ms_command_sizes[param.cmd].outsize,
resp, ms_command_sizes[param.cmd].insize);
if (rv < 0)
return rv;
printf("Offset vector: X:%d, Y:%d, Z:%d\n",
resp->sensor_offset.offset[0],
resp->sensor_offset.offset[1],
resp->sensor_offset.offset[2]);
if ((uint16_t)resp->sensor_offset.temp ==
EC_MOTION_SENSE_INVALID_CALIB_TEMP)
printf("temperature at calibration unknown\n");
else
printf("temperature at calibration: %d.%02d C\n",
resp->sensor_offset.temp / 100,
resp->sensor_offset.temp % 100);
return 0;
}
if (argc == 3 && !strcasecmp(argv[1], "list_activities")) {
param.cmd = MOTIONSENSE_CMD_LIST_ACTIVITIES;
param.list_activities.sensor_num = strtol(argv[2], &e, 0);
rv = ec_command(EC_CMD_MOTION_SENSE_CMD, 2,
¶m, ms_command_sizes[param.cmd].outsize,
resp, ms_command_sizes[param.cmd].insize);
if (rv < 0)
return rv;
printf("Enabled:\n");
motionsense_display_activities(resp->list_activities.enabled);
printf("Disabled:\n");
motionsense_display_activities(resp->list_activities.disabled);
return 0;
}
if (argc == 5 && !strcasecmp(argv[1], "set_activity")) {
param.cmd = MOTIONSENSE_CMD_SET_ACTIVITY;
param.set_activity.sensor_num = strtol(argv[2], &e, 0);
param.set_activity.activity = strtol(argv[3], &e, 0);
param.set_activity.enable = strtol(argv[4], &e, 0);
rv = ec_command(EC_CMD_MOTION_SENSE_CMD, 2,
¶m, ms_command_sizes[param.cmd].outsize,
resp, ms_command_sizes[param.cmd].insize);
if (rv < 0)
return rv;
return 0;
}
if (argc == 2 && !strcasecmp(argv[1], "lid_angle")) {
param.cmd = MOTIONSENSE_CMD_LID_ANGLE;
rv = ec_command(EC_CMD_MOTION_SENSE_CMD, 2,
¶m, ms_command_sizes[param.cmd].outsize,
resp, ms_command_sizes[param.cmd].insize);
if (rv < 0)
return rv;
printf("Lid angle: ");
if (resp->lid_angle.value == LID_ANGLE_UNRELIABLE)
printf("unreliable\n");
else
printf("%d\n", resp->lid_angle.value);
return 0;
}
if (argc >= 3 && !strcasecmp(argv[1], "spoof")) {
param.cmd = MOTIONSENSE_CMD_SPOOF;
/* By default, just query the current spoof status. */
param.spoof.spoof_enable = MOTIONSENSE_SPOOF_MODE_QUERY;
param.spoof.sensor_id = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad %s arg.\n", argv[2]);
return -1;
}
if (argc >= 4) {
int enable, i;
int16_t val;
enable = strtol(argv[3], &e, 0);
if ((e && *e) || (enable != 0 && enable != 1)) {
fprintf(stderr, "Bad %s arg.\n", argv[3]);
return -1;
}
if ((enable == 1) && (argc == 4)) {
/*
* Enable spoofing, but lock to current sensor
* values.
*/
param.spoof.spoof_enable =
MOTIONSENSE_SPOOF_MODE_LOCK_CURRENT;
} else if ((enable == 1) && (argc == 7)) {
/*
* Enable spoofing, but use provided component
* values.
*/
param.spoof.spoof_enable =
MOTIONSENSE_SPOOF_MODE_CUSTOM;
for (i = 0; i < 3; i++) {
val = strtol(argv[4+i], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad %s arg.\n",
argv[4+i]);
return -1;
}
param.spoof.components[i] = val;
}
} else if (enable == 0) {
param.spoof.spoof_enable =
MOTIONSENSE_SPOOF_MODE_DISABLE;
} else {
return ms_help(argv[0]);
}
}
rv = ec_command(EC_CMD_MOTION_SENSE_CMD, 2,
¶m, ms_command_sizes[param.cmd].outsize,
resp, ms_command_sizes[param.cmd].insize);
if (rv < 0)
return rv;
if (param.spoof.spoof_enable == MOTIONSENSE_SPOOF_MODE_QUERY)
/*
* Response is the current spoof status of the
* sensor.
*/
printf("Sensor %d spoof mode is %s.\n",
param.spoof.sensor_id,
resp->spoof.ret ? "enabled" : "disabled");
return 0;
}
return ms_help(argv[0]);
}
int cmd_next_event(int argc, char *argv[])
{
uint8_t *rdata = (uint8_t *)ec_inbuf;
int rv;
int i;
rv = ec_command(EC_CMD_GET_NEXT_EVENT, 0,
NULL, 0, rdata, ec_max_insize);
if (rv < 0)
return rv;
printf("Next event is 0x%02x\n", rdata[0]);
if (rv > 1) {
printf("Event data:\n");
for (i = 1; i < rv; ++i) {
printf("%02x ", rdata[i]);
if (!(i & 0xf))
printf("\n");
}
printf("\n");
}
return 0;
}
static int find_led_color_by_name(const char *color)
{
int i;
for (i = 0; i < EC_LED_COLOR_COUNT; ++i)
if (!strcasecmp(color, led_color_names[i]))
return i;
return -1;
}
static int find_led_id_by_name(const char *led)
{
int i;
for (i = 0; i < EC_LED_ID_COUNT; ++i)
if (!strcasecmp(led, led_names[i]))
return i;
return -1;
}
int cmd_led(int argc, char *argv[])
{
struct ec_params_led_control p;
struct ec_response_led_control r;
char *e, *ptr;
int rv, i, j;
memset(p.brightness, 0, sizeof(p.brightness));
p.flags = 0;
if (argc < 3) {
fprintf(stderr,
"Usage: %s <name> <query | auto | "
"off | <color> | <color>=<value>...>\n", argv[0]);
return -1;
}
p.led_id = find_led_id_by_name(argv[1]);
if (p.led_id == (uint8_t)-1) {
fprintf(stderr, "Bad LED name: %s\n", argv[1]);
fprintf(stderr, "Valid LED names: ");
for (i = 0; i < EC_LED_ID_COUNT; i++)
fprintf(stderr, "%s ", led_names[i]);
fprintf(stderr, "\n");
return -1;
}
if (!strcasecmp(argv[2], "query")) {
p.flags = EC_LED_FLAGS_QUERY;
rv = ec_command(EC_CMD_LED_CONTROL, 1, &p, sizeof(p),
&r, sizeof(r));
printf("Brightness range for LED %d:\n", p.led_id);
if (rv < 0) {
fprintf(stderr, "Error: Unsupported LED.\n");
return rv;
}
for (i = 0; i < EC_LED_COLOR_COUNT; ++i)
printf("\t%s\t: 0x%x\n",
led_color_names[i],
r.brightness_range[i]);
return 0;
}
if (!strcasecmp(argv[2], "off")) {
/* Brightness initialized to 0 for each color. */
} else if (!strcasecmp(argv[2], "auto")) {
p.flags = EC_LED_FLAGS_AUTO;
} else if ((i = find_led_color_by_name(argv[2])) != -1) {
p.brightness[i] = 0xff;
} else {
for (i = 2; i < argc; ++i) {
ptr = strtok(argv[i], "=");
j = find_led_color_by_name(ptr);
if (j == -1) {
fprintf(stderr, "Bad color name: %s\n", ptr);
fprintf(stderr, "Valid colors: ");
for (j = 0; j < EC_LED_COLOR_COUNT; j++)
fprintf(stderr, "%s ",
led_color_names[j]);
fprintf(stderr, "\n");
return -1;
}
ptr = strtok(NULL, "=");
if (ptr == NULL) {
fprintf(stderr, "Missing brightness value\n");
return -1;
}
p.brightness[j] = strtol(ptr, &e, 0);
if (e && *e) {
fprintf(stderr, "Bad brightness: %s\n", ptr);
return -1;
}
}
}
rv = ec_command(EC_CMD_LED_CONTROL, 1, &p, sizeof(p), &r, sizeof(r));
return (rv < 0 ? rv : 0);
}
int cmd_usb_charge_set_mode(int argc, char *argv[])
{
struct ec_params_usb_charge_set_mode p;
char *e;
int rv;
if (argc != 3 && argc != 4) {
fprintf(stderr,
"Usage: %s <port_id> <mode_id> [<inhibit_charge>]\n",
argv[0]);
return -1;
}
p.usb_port_id = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad port ID.\n");
return -1;
}
p.mode = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad mode ID.\n");
return -1;
}
p.inhibit_charge = 0;
if (argc == 4) {
p.inhibit_charge = strtol(argv[3], &e, 0);
if ((e && *e) || (p.inhibit_charge != 0 &&
p.inhibit_charge != 1)) {
fprintf(stderr, "Bad value\n");
return -1;
}
}
printf("Setting port %d to mode %d inhibit_charge %d...\n",
p.usb_port_id, p.mode, p.inhibit_charge);
rv = ec_command(EC_CMD_USB_CHARGE_SET_MODE, 0,
&p, sizeof(p), NULL, 0);
if (rv < 0)
return rv;
printf("USB charging mode set.\n");
return 0;
}
int cmd_usb_mux(int argc, char *argv[])
{
struct ec_params_usb_mux p;
char *e;
int rv;
if (argc != 2) {
fprintf(stderr, "Usage: %s <mux>\n", argv[0]);
return -1;
}
p.mux = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad mux value.\n");
return -1;
}
rv = ec_command(EC_CMD_USB_MUX, 0,
&p, sizeof(p), NULL, 0);
if (rv < 0)
return rv;
printf("Set USB mux to 0x%x.\n", p.mux);
return 0;
}
int cmd_usb_pd(int argc, char *argv[])
{
const char *role_str[] = {"", "toggle", "toggle-off", "sink", "source",
"freeze"};
const char *mux_str[] = {"", "none", "usb", "dp", "dock", "auto"};
const char *swap_str[] = {"", "dr_swap", "pr_swap", "vconn_swap"};
struct ec_params_usb_pd_control p;
struct ec_response_usb_pd_control_v2 *r_v2 =
(struct ec_response_usb_pd_control_v2 *)ec_inbuf;
struct ec_response_usb_pd_control_v1 *r_v1 =
(struct ec_response_usb_pd_control_v1 *)ec_inbuf;
struct ec_response_usb_pd_control *r =
(struct ec_response_usb_pd_control *)ec_inbuf;
int rv, i, j;
int option_ok;
char *e;
int cmdver;
BUILD_ASSERT(ARRAY_SIZE(role_str) == USB_PD_CTRL_ROLE_COUNT);
BUILD_ASSERT(ARRAY_SIZE(mux_str) == USB_PD_CTRL_MUX_COUNT);
BUILD_ASSERT(ARRAY_SIZE(swap_str) == USB_PD_CTRL_SWAP_COUNT);
p.role = USB_PD_CTRL_ROLE_NO_CHANGE;
p.mux = USB_PD_CTRL_MUX_NO_CHANGE;
p.swap = USB_PD_CTRL_SWAP_NONE;
if (argc < 2) {
fprintf(stderr, "No port specified.\n");
return -1;
}
p.port = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Invalid param (port)\n");
return -1;
}
for (i = 2; i < argc; ++i) {
option_ok = 0;
if (!strcmp(argv[i], "auto")) {
if (argc != 3) {
fprintf(stderr, "\"auto\" may not be used "
"with other options.\n");
return -1;
}
p.role = USB_PD_CTRL_ROLE_TOGGLE_ON;
p.mux = USB_PD_CTRL_MUX_AUTO;
continue;
}
for (j = 0; j < ARRAY_SIZE(role_str); ++j) {
if (!strcmp(argv[i], role_str[j])) {
if (p.role != USB_PD_CTRL_ROLE_NO_CHANGE) {
fprintf(stderr,
"Only one role allowed.\n");
return -1;
}
p.role = j;
option_ok = 1;
break;
}
}
if (option_ok)
continue;
for (j = 0; j < ARRAY_SIZE(mux_str); ++j) {
if (!strcmp(argv[i], mux_str[j])) {
if (p.mux != USB_PD_CTRL_MUX_NO_CHANGE) {
fprintf(stderr,
"Only one mux type allowed.\n");
return -1;
}
p.mux = j;
option_ok = 1;
break;
}
}
if (option_ok)
continue;
for (j = 0; j < ARRAY_SIZE(swap_str); ++j) {
if (!strcmp(argv[i], swap_str[j])) {
if (p.swap != USB_PD_CTRL_SWAP_NONE) {
fprintf(stderr,
"Only one swap type allowed.\n");
return -1;
}
p.swap = j;
option_ok = 1;
break;
}
}
if (!option_ok) {
fprintf(stderr, "Unknown option: %s\n", argv[i]);
return -1;
}
}
if (ec_cmd_version_supported(EC_CMD_USB_PD_CONTROL, 2))
cmdver = 2;
else if (ec_cmd_version_supported(EC_CMD_USB_PD_CONTROL, 1))
cmdver = 1;
else
cmdver = 0;
rv = ec_command(EC_CMD_USB_PD_CONTROL, cmdver, &p, sizeof(p),
ec_inbuf, ec_max_insize);
if (rv < 0 || argc != 2)
return (rv < 0) ? rv : 0;
if (cmdver == 0) {
printf("Port C%d is %sabled, Role:%s Polarity:CC%d State:%d\n",
p.port, (r->enabled) ? "en" : "dis",
r->role == PD_ROLE_SOURCE ? "SRC" : "SNK",
r->polarity + 1, r->state);
} else {
printf("Port C%d: %s, %s State:%s\n"
"Role:%s %s%s, Polarity:CC%d\n",
p.port,
(r_v1->enabled & PD_CTRL_RESP_ENABLED_COMMS) ?
"enabled" : "disabled",
(r_v1->enabled & PD_CTRL_RESP_ENABLED_CONNECTED) ?
"connected" : "disconnected",
r_v1->state,
(r_v1->role & PD_CTRL_RESP_ROLE_POWER) ? "SRC" : "SNK",
(r_v1->role & PD_CTRL_RESP_ROLE_DATA) ? "DFP" : "UFP",
(r_v1->role & PD_CTRL_RESP_ROLE_VCONN) ? " VCONN" : "",
r_v1->polarity + 1);
if (cmdver == 2) {
printf("CC State:");
if (r_v2->cc_state == USBC_PD_CC_NONE)
printf("None");
else if (r_v2->cc_state == USBC_PD_CC_NO_UFP)
printf("No UFP");
else if (r_v2->cc_state == USBC_PD_CC_AUDIO_ACC)
printf("Audio accessory");
else if (r_v2->cc_state == USBC_PD_CC_DEBUG_ACC)
printf("Debug accessory");
else if (r_v2->cc_state == USBC_PD_CC_UFP_ATTACHED)
printf("UFP attached");
else if (r_v2->cc_state == USBC_PD_CC_DFP_ATTACHED)
printf("DFP attached");
else
printf("UNKNOWN");
printf("\n");
if (r_v2->dp_mode) {
printf("DP pin mode:");
if (r_v2->dp_mode == MODE_DP_PIN_A)
printf("A");
else if (r_v2->dp_mode == MODE_DP_PIN_B)
printf("B");
else if (r_v2->dp_mode == MODE_DP_PIN_C)
printf("C");
else if (r_v2->dp_mode == MODE_DP_PIN_D)
printf("D");
else if (r_v2->dp_mode == MODE_DP_PIN_E)
printf("E");
else if (r_v2->dp_mode == MODE_DP_PIN_F)
printf("F");
else
printf("UNKNOWN");
printf("\n");
}
}
/* If connected to a PD device, then print port partner info */
if ((r_v1->enabled & PD_CTRL_RESP_ENABLED_CONNECTED) &&
(r_v1->enabled & PD_CTRL_RESP_ENABLED_PD_CAPABLE))
printf("PD Partner Capabilities:\n%s%s%s%s",
(r_v1->role & PD_CTRL_RESP_ROLE_DR_POWER) ?
" DR power\n" : "",
(r_v1->role & PD_CTRL_RESP_ROLE_DR_DATA) ?
" DR data\n" : "",
(r_v1->role & PD_CTRL_RESP_ROLE_USB_COMM) ?
" USB capable\n" : "",
(r_v1->role & PD_CTRL_RESP_ROLE_EXT_POWERED) ?
" Externally powered\n" : "");
}
return 0;
}
static void print_pd_power_info(struct ec_response_usb_pd_power_info *r)
{
switch (r->role) {
case USB_PD_PORT_POWER_DISCONNECTED:
printf("Disconnected");
break;
case USB_PD_PORT_POWER_SOURCE:
printf("SRC");
break;
case USB_PD_PORT_POWER_SINK:
printf("SNK");
break;
case USB_PD_PORT_POWER_SINK_NOT_CHARGING:
printf("SNK (not charging)");
break;
default:
printf("Unknown");
}
if ((r->role == USB_PD_PORT_POWER_SOURCE) &&
(r->meas.current_max))
printf(" %dmA", r->meas.current_max);
if ((r->role == USB_PD_PORT_POWER_DISCONNECTED) ||
(r->role == USB_PD_PORT_POWER_SOURCE)) {
printf("\n");
return;
}
printf(r->dualrole ? " DRP" : " Charger");
switch (r->type) {
case USB_CHG_TYPE_PD:
printf(" PD");
break;
case USB_CHG_TYPE_C:
printf(" Type-C");
break;
case USB_CHG_TYPE_PROPRIETARY:
printf(" Proprietary");
break;
case USB_CHG_TYPE_BC12_DCP:
printf(" DCP");
break;
case USB_CHG_TYPE_BC12_CDP:
printf(" CDP");
break;
case USB_CHG_TYPE_BC12_SDP:
printf(" SDP");
break;
case USB_CHG_TYPE_OTHER:
printf(" Other");
break;
case USB_CHG_TYPE_VBUS:
printf(" VBUS");
break;
case USB_CHG_TYPE_UNKNOWN:
printf(" Unknown");
break;
}
printf(" %dmV / %dmA, max %dmV / %dmA",
r->meas.voltage_now, r->meas.current_lim, r->meas.voltage_max,
r->meas.current_max);
if (r->max_power)
printf(" / %dmW", r->max_power / 1000);
printf("\n");
}
int cmd_usb_pd_mux_info(int argc, char *argv[])
{
struct ec_params_usb_pd_mux_info p;
struct ec_response_usb_pd_mux_info r;
int num_ports, rv, i;
rv = ec_command(EC_CMD_USB_PD_PORTS, 0, NULL, 0,
ec_inbuf, ec_max_insize);
if (rv < 0)
return rv;
num_ports = ((struct ec_response_usb_pd_ports *)ec_inbuf)->num_ports;
for (i = 0; i < num_ports; i++) {
p.port = i;
rv = ec_command(EC_CMD_USB_PD_MUX_INFO, 0,
&p, sizeof(p),
&r, sizeof(r));
if (rv < 0)
return rv;
printf("Port %d: ", i);
if (r.flags & USB_PD_MUX_USB_ENABLED)
printf("USB ");
if (r.flags & USB_PD_MUX_DP_ENABLED)
printf("DP ");
if (!(r.flags & (USB_PD_MUX_DP_ENABLED |
USB_PD_MUX_USB_ENABLED)))
printf("OPEN ");
if (r.flags & USB_PD_MUX_POLARITY_INVERTED)
printf("INV ");
if (r.flags & USB_PD_MUX_HPD_IRQ)
printf("HPD_IRQ ");
printf("\n");
}
return 0;
}
int cmd_usb_pd_power(int argc, char *argv[])
{
struct ec_params_usb_pd_power_info p;
struct ec_response_usb_pd_power_info *r =
(struct ec_response_usb_pd_power_info *)ec_inbuf;
int num_ports, i, rv;
char *e;
rv = ec_command(EC_CMD_USB_PD_PORTS, 0, NULL, 0,
ec_inbuf, ec_max_insize);
if (rv < 0)
return rv;
num_ports = ((struct ec_response_usb_pd_ports *)r)->num_ports;
if (argc < 2) {
for (i = 0; i < num_ports; i++) {
p.port = i;
rv = ec_command(EC_CMD_USB_PD_POWER_INFO, 0,
&p, sizeof(p),
ec_inbuf, ec_max_insize);
if (rv < 0)
return rv;
printf("Port %d: ", i);
print_pd_power_info(r);
}
} else {
p.port = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad port.\n");
return -1;
}
rv = ec_command(EC_CMD_USB_PD_POWER_INFO, 0,
&p, sizeof(p),
ec_inbuf, ec_max_insize);
if (rv < 0)
return rv;
printf("Port %d: ", p.port);
print_pd_power_info(r);
}
return 0;
}
int cmd_kbpress(int argc, char *argv[])
{
struct ec_params_mkbp_simulate_key p;
char *e;
int rv;
if (argc != 4) {
fprintf(stderr,
"Usage: %s <row> <col> <0|1>\n", argv[0]);
return -1;
}
p.row = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad row.\n");
return -1;
}
p.col = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad column.\n");
return -1;
}
p.pressed = strtol(argv[3], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad pressed flag.\n");
return -1;
}
printf("%s row %d col %d.\n", p.pressed ? "Pressing" : "Releasing",
p.row,
p.col);
rv = ec_command(EC_CMD_MKBP_SIMULATE_KEY, 0,
&p, sizeof(p), NULL, 0);
if (rv < 0)
return rv;
printf("Done.\n");
return 0;
}
int cmd_keyboard_factory_test(int argc, char *argv[])
{
struct ec_response_keyboard_factory_test r;
int rv;
rv = ec_command(EC_CMD_KEYBOARD_FACTORY_TEST, 0,
NULL, 0, &r, sizeof(r));
if (rv < 0)
return rv;
if (r.shorted != 0)
printf("Keyboard %d and %d pin are shorted.\n",
r.shorted & 0x00ff, r.shorted >> 8);
else
printf("Keyboard factory test passed.\n");
return 0;
}
int cmd_panic_info(int argc, char *argv[])
{
int rv;
struct panic_data *pdata = (struct panic_data *)ec_inbuf;
rv = ec_command(EC_CMD_GET_PANIC_INFO, 0, NULL, 0,
ec_inbuf, ec_max_insize);
if (rv < 0)
return rv;
if (rv == 0) {
printf("No panic data.\n");
return 0;
}
return parse_panic_info(pdata);
}
int cmd_power_info(int argc, char *argv[])
{
struct ec_response_power_info r;
int rv;
rv = ec_command(EC_CMD_POWER_INFO, 0, NULL, 0, &r, sizeof(r));
if (rv < 0)
return rv;
printf("AC Voltage: %d mV\n", r.voltage_ac);
printf("System Voltage: %d mV\n", r.voltage_system);
printf("System Current: %d mA\n", r.current_system);
printf("System Power: %d mW\n",
r.voltage_system * r.current_system / 1000);
printf("USB Device Type: 0x%x\n", r.usb_dev_type);
printf("USB Current Limit: %d mA\n", r.usb_current_limit);
return 0;
}
int cmd_pstore_info(int argc, char *argv[])
{
struct ec_response_pstore_info r;
int rv;
rv = ec_command(EC_CMD_PSTORE_INFO, 0, NULL, 0, &r, sizeof(r));
if (rv < 0)
return rv;
printf("PstoreSize %d\nAccessSize %d\n", r.pstore_size, r.access_size);
return 0;
}
int cmd_pstore_read(int argc, char *argv[])
{
struct ec_params_pstore_read p;
uint8_t rdata[EC_PSTORE_SIZE_MAX];
int offset, size;
int rv;
int i;
char *e;
char *buf;
if (argc < 4) {
fprintf(stderr,
"Usage: %s <offset> <size> <filename>\n", argv[0]);
return -1;
}
offset = strtol(argv[1], &e, 0);
if ((e && *e) || offset < 0 || offset > 0x10000) {
fprintf(stderr, "Bad offset.\n");
return -1;
}
size = strtol(argv[2], &e, 0);
if ((e && *e) || size <= 0 || size > 0x10000) {
fprintf(stderr, "Bad size.\n");
return -1;
}
printf("Reading %d bytes at offset %d...\n", size, offset);
buf = (char *)malloc(size);
if (!buf) {
fprintf(stderr, "Unable to allocate buffer.\n");
return -1;
}
/* Read data in chunks */
for (i = 0; i < size; i += EC_PSTORE_SIZE_MAX) {
p.offset = offset + i;
p.size = MIN(size - i, EC_PSTORE_SIZE_MAX);
rv = ec_command(EC_CMD_PSTORE_READ, 0,
&p, sizeof(p), rdata, sizeof(rdata));
if (rv < 0) {
fprintf(stderr, "Read error at offset %d\n", i);
free(buf);
return rv;
}
memcpy(buf + i, rdata, p.size);
}
rv = write_file(argv[3], buf, size);
free(buf);
if (rv)
return rv;
printf("done.\n");
return 0;
}
int cmd_pstore_write(int argc, char *argv[])
{
struct ec_params_pstore_write p;
int offset, size;
int rv;
int i;
char *e;
char *buf;
if (argc < 3) {
fprintf(stderr, "Usage: %s <offset> <filename>\n", argv[0]);
return -1;
}
offset = strtol(argv[1], &e, 0);
if ((e && *e) || offset < 0 || offset > 0x10000) {
fprintf(stderr, "Bad offset.\n");
return -1;
}
/* Read the input file */
buf = read_file(argv[2], &size);
if (!buf)
return -1;
printf("Writing to offset %d...\n", offset);
/* Write data in chunks */
for (i = 0; i < size; i += EC_PSTORE_SIZE_MAX) {
p.offset = offset + i;
p.size = MIN(size - i, EC_PSTORE_SIZE_MAX);
memcpy(p.data, buf + i, p.size);
rv = ec_command(EC_CMD_PSTORE_WRITE, 0,
&p, sizeof(p), NULL, 0);
if (rv < 0) {
fprintf(stderr, "Write error at offset %d\n", i);
free(buf);
return rv;
}
}
free(buf);
printf("done.\n");
return 0;
}
int cmd_host_event_get_raw(int argc, char *argv[])
{
uint32_t events = read_mapped_mem32(EC_MEMMAP_HOST_EVENTS);
if (events & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID)) {
printf("Current host events: invalid\n");
return -1;
}
printf("Current host events: 0x%08x\n", events);
return 0;
}
int cmd_host_event_get_b(int argc, char *argv[])
{
struct ec_response_host_event_mask r;
int rv;
rv = ec_command(EC_CMD_HOST_EVENT_GET_B, 0,
NULL, 0, &r, sizeof(r));
if (rv < 0)
return rv;
if (rv < sizeof(r)) {
fprintf(stderr, "Insufficient data received.\n");
return -1;
}
if (r.mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID)) {
printf("Current host events-B: invalid\n");
return -1;
}
printf("Current host events-B: 0x%08x\n", r.mask);
return 0;
}
int cmd_host_event_get_smi_mask(int argc, char *argv[])
{
struct ec_response_host_event_mask r;
int rv;
rv = ec_command(EC_CMD_HOST_EVENT_GET_SMI_MASK, 0,
NULL, 0, &r, sizeof(r));
if (rv < 0)
return rv;
printf("Current host event SMI mask: 0x%08x\n", r.mask);
return 0;
}
int cmd_host_event_get_sci_mask(int argc, char *argv[])
{
struct ec_response_host_event_mask r;
int rv;
rv = ec_command(EC_CMD_HOST_EVENT_GET_SCI_MASK, 0,
NULL, 0, &r, sizeof(r));
if (rv < 0)
return rv;
printf("Current host event SCI mask: 0x%08x\n", r.mask);
return 0;
}
int cmd_host_event_get_wake_mask(int argc, char *argv[])
{
struct ec_response_host_event_mask r;
int rv;
rv = ec_command(EC_CMD_HOST_EVENT_GET_WAKE_MASK, 0,
NULL, 0, &r, sizeof(r));
if (rv < 0)
return rv;
printf("Current host event wake mask: 0x%08x\n", r.mask);
return 0;
}
int cmd_host_event_set_smi_mask(int argc, char *argv[])
{
struct ec_params_host_event_mask p;
char *e;
int rv;
if (argc != 2) {
fprintf(stderr, "Usage: %s <mask>\n", argv[0]);
return -1;
}
p.mask = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad mask.\n");
return -1;
}
rv = ec_command(EC_CMD_HOST_EVENT_SET_SMI_MASK, 0,
&p, sizeof(p), NULL, 0);
if (rv < 0)
return rv;
printf("Mask set.\n");
return 0;
}
int cmd_host_event_set_sci_mask(int argc, char *argv[])
{
struct ec_params_host_event_mask p;
char *e;
int rv;
if (argc != 2) {
fprintf(stderr, "Usage: %s <mask>\n", argv[0]);
return -1;
}
p.mask = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad mask.\n");
return -1;
}
rv = ec_command(EC_CMD_HOST_EVENT_SET_SCI_MASK, 0,
&p, sizeof(p), NULL, 0);
if (rv < 0)
return rv;
printf("Mask set.\n");
return 0;
}
int cmd_host_event_set_wake_mask(int argc, char *argv[])
{
struct ec_params_host_event_mask p;
char *e;
int rv;
if (argc != 2) {
fprintf(stderr, "Usage: %s <mask>\n", argv[0]);
return -1;
}
p.mask = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad mask.\n");
return -1;
}
rv = ec_command(EC_CMD_HOST_EVENT_SET_WAKE_MASK, 0,
&p, sizeof(p), NULL, 0);
if (rv < 0)
return rv;
printf("Mask set.\n");
return 0;
}
int cmd_host_event_clear(int argc, char *argv[])
{
struct ec_params_host_event_mask p;
char *e;
int rv;
if (argc != 2) {
fprintf(stderr, "Usage: %s <mask>\n", argv[0]);
return -1;
}
p.mask = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad mask.\n");
return -1;
}
rv = ec_command(EC_CMD_HOST_EVENT_CLEAR, 0,
&p, sizeof(p), NULL, 0);
if (rv < 0)
return rv;
printf("Host events cleared.\n");
return 0;
}
int cmd_host_event_clear_b(int argc, char *argv[])
{
struct ec_params_host_event_mask p;
char *e;
int rv;
if (argc != 2) {
fprintf(stderr, "Usage: %s <mask>\n", argv[0]);
return -1;
}
p.mask = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad mask.\n");
return -1;
}
rv = ec_command(EC_CMD_HOST_EVENT_CLEAR_B, 0,
&p, sizeof(p), NULL, 0);
if (rv < 0)
return rv;
printf("Host events-B cleared.\n");
return 0;
}
int cmd_switches(int argc, char *argv[])
{
uint8_t s = read_mapped_mem8(EC_MEMMAP_SWITCHES);
printf("Current switches: 0x%02x\n", s);
printf("Lid switch: %s\n",
(s & EC_SWITCH_LID_OPEN ? "OPEN" : "CLOSED"));
printf("Power button: %s\n",
(s & EC_SWITCH_POWER_BUTTON_PRESSED ? "DOWN" : "UP"));
printf("Write protect: %sABLED\n",
(s & EC_SWITCH_WRITE_PROTECT_DISABLED ? "DIS" : "EN"));
printf("Dedicated recovery: %sABLED\n",
(s & EC_SWITCH_DEDICATED_RECOVERY ? "EN" : "DIS"));
return 0;
}
int cmd_wireless(int argc, char *argv[])
{
char *e;
int rv;
int now_flags;
if (argc < 2) {
fprintf(stderr,
"Usage: %s <flags> [<mask> [<susflags> <susmask>]]\n",
argv[0]);
fprintf(stderr, " 0x1 = WLAN radio\n"
" 0x2 = Bluetooth radio\n"
" 0x4 = WWAN power\n"
" 0x8 = WLAN power\n");
return -1;
}
now_flags = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad flags.\n");
return -1;
}
if (argc < 3) {
/* Old-style - current flags only */
struct ec_params_switch_enable_wireless_v0 p;
p.enabled = now_flags;
rv = ec_command(EC_CMD_SWITCH_ENABLE_WIRELESS, 0,
&p, sizeof(p), NULL, 0);
if (rv < 0)
return rv;
printf("Success.\n");
} else {
/* New-style - masks and suspend flags */
struct ec_params_switch_enable_wireless_v1 p;
struct ec_response_switch_enable_wireless_v1 r;
memset(&p, 0, sizeof(p));
p.now_flags = now_flags;
p.now_mask = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad mask.\n");
return -1;
}
if (argc > 4) {
p.suspend_flags = strtol(argv[3], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad suspend flags.\n");
return -1;
}
p.suspend_mask = strtol(argv[4], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad suspend mask.\n");
return -1;
}
}
rv = ec_command(EC_CMD_SWITCH_ENABLE_WIRELESS,
EC_VER_SWITCH_ENABLE_WIRELESS,
&p, sizeof(p), &r, sizeof(r));
if (rv < 0)
return rv;
printf("Now=0x%x, suspend=0x%x\n",
r.now_flags, r.suspend_flags);
}
return 0;
}
int cmd_i2c_protect(int argc, char *argv[])
{
struct ec_params_i2c_passthru_protect p;
char *e;
int rv;
if (argc != 2 && (argc != 3 || strcmp(argv[2], "status"))) {
fprintf(stderr, "Usage: %s <port> [status]\n",
argv[0]);
return -1;
}
p.port = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad port.\n");
return -1;
}
if (argc == 3) {
struct ec_response_i2c_passthru_protect r;
p.subcmd = EC_CMD_I2C_PASSTHRU_PROTECT_STATUS;
rv = ec_command(EC_CMD_I2C_PASSTHRU_PROTECT, 0, &p, sizeof(p),
&r, sizeof(r));
if (rv < 0)
return rv;
printf("I2C port %d: %s (%d)\n", p.port,
r.status ? "Protected" : "Unprotected", r.status);
} else {
p.subcmd = EC_CMD_I2C_PASSTHRU_PROTECT_ENABLE;
rv = ec_command(EC_CMD_I2C_PASSTHRU_PROTECT, 0, &p, sizeof(p),
NULL, 0);
if (rv < 0)
return rv;
}
return 0;
}
int do_i2c_xfer(unsigned int port, unsigned int addr,
uint8_t *write_buf, int write_len,
uint8_t **read_buf, int read_len) {
struct ec_params_i2c_passthru *p =
(struct ec_params_i2c_passthru *)ec_outbuf;
struct ec_response_i2c_passthru *r =
(struct ec_response_i2c_passthru *)ec_inbuf;
struct ec_params_i2c_passthru_msg *msg = p->msg;
uint8_t *pdata;
int size;
int rv;
p->port = port;
p->num_msgs = (read_len != 0) + (write_len != 0);
size = sizeof(*p) + p->num_msgs * sizeof(*msg);
if (size + write_len > ec_max_outsize) {
fprintf(stderr, "Params too large for buffer\n");
return -1;
}
if (sizeof(*r) + read_len > ec_max_insize) {
fprintf(stderr, "Read length too big for buffer\n");
return -1;
}
pdata = (uint8_t *)p + size;
if (write_len) {
msg->addr_flags = addr;
msg->len = write_len;
memcpy(pdata, write_buf, write_len);
msg++;
}
if (read_len) {
msg->addr_flags = addr | EC_I2C_FLAG_READ;
msg->len = read_len;
}
rv = ec_command(EC_CMD_I2C_PASSTHRU, 0, p, size + write_len,
r, sizeof(*r) + read_len);
if (rv < 0)
return rv;
/* Parse response */
if (r->i2c_status & (EC_I2C_STATUS_NAK | EC_I2C_STATUS_TIMEOUT)) {
fprintf(stderr, "Transfer failed with status=0x%x\n",
r->i2c_status);
return -1;
}
if (rv < sizeof(*r) + read_len) {
fprintf(stderr, "Truncated read response\n");
return -1;
}
if (read_len)
*read_buf = r->data;
return 0;
}
int cmd_i2c_read(int argc, char *argv[])
{
unsigned int port, addr;
int read_len, write_len;
uint8_t write_buf[1];
uint8_t *read_buf = NULL;
char *e;
int rv;
if (argc != 5) {
fprintf(stderr, "Usage: %s <8 | 16> <port> <addr> <offset>\n",
argv[0]);
return -1;
}
read_len = strtol(argv[1], &e, 0);
if ((e && *e) || (read_len != 8 && read_len != 16)) {
fprintf(stderr, "Bad read size.\n");
return -1;
}
read_len = read_len / 8;
port = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad port.\n");
return -1;
}
addr = strtol(argv[3], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad address.\n");
return -1;
}
/* Convert from 8-bit to 7-bit address */
addr = addr >> 1;
write_buf[0] = strtol(argv[4], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad offset.\n");
return -1;
}
write_len = 1;
rv = do_i2c_xfer(port, addr, write_buf, write_len, &read_buf, read_len);
if (rv < 0)
return rv;
printf("Read from I2C port %d at 0x%x offset 0x%x = 0x%x\n",
port, addr, write_buf[0], *(uint16_t *)read_buf);
return 0;
}
int cmd_i2c_write(int argc, char *argv[])
{
unsigned int port, addr;
int write_len;
uint8_t write_buf[3];
char *e;
int rv;
if (argc != 6) {
fprintf(stderr,
"Usage: %s <8 | 16> <port> <addr> <offset> <data>\n",
argv[0]);
return -1;
}
write_len = strtol(argv[1], &e, 0);
if ((e && *e) || (write_len != 8 && write_len != 16)) {
fprintf(stderr, "Bad write size.\n");
return -1;
}
/* Include offset (length 1) */
write_len = 1 + write_len / 8;
port = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad port.\n");
return -1;
}
addr = strtol(argv[3], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad address.\n");
return -1;
}
/* Convert from 8-bit to 7-bit address */
addr = addr >> 1;
write_buf[0] = strtol(argv[4], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad offset.\n");
return -1;
}
*((uint16_t *)&write_buf[1]) = strtol(argv[5], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad data.\n");
return -1;
}
rv = do_i2c_xfer(port, addr, write_buf, write_len, NULL, 0);
if (rv < 0)
return rv;
printf("Wrote 0x%x to I2C port %d at 0x%x offset 0x%x.\n",
*((uint16_t *)&write_buf[1]), port, addr, write_buf[0]);
return 0;
}
int cmd_i2c_xfer(int argc, char *argv[])
{
unsigned int port, addr;
int read_len, write_len;
uint8_t *write_buf = NULL;
uint8_t *read_buf;
char *e;
int rv, i;
if (argc < 4) {
fprintf(stderr,
"Usage: %s <port> <slave_addr> <read_count> "
"[write bytes...]\n", argv[0]);
return -1;
}
port = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad port.\n");
return -1;
}
addr = strtol(argv[2], &e, 0) & 0x7f;
if (e && *e) {
fprintf(stderr, "Bad slave address.\n");
return -1;
}
read_len = strtol(argv[3], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad read length.\n");
return -1;
}
/* Skip over params to bytes to write */
argc -= 4;
argv += 4;
write_len = argc;
if (write_len) {
write_buf = malloc(write_len);
for (i = 0; i < write_len; i++) {
write_buf[i] = strtol(argv[i], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad write byte %d\n", i);
return -1;
}
}
}
rv = do_i2c_xfer(port, addr, write_buf, write_len, &read_buf, read_len);
if (write_len)
free(write_buf);
if (rv)
return rv;
if (read_len) {
if (ascii_mode) {
for (i = 0; i < read_len; i++)
printf(isprint(read_buf[i]) ? "%c" : "\\x%02x",
read_buf[i]);
} else {
printf("Read bytes:");
for (i = 0; i < read_len; i++)
printf(" %#02x", read_buf[i]);
}
printf("\n");
} else {
printf("Write successful.\n");
}
return 0;
}
int cmd_lcd_backlight(int argc, char *argv[])
{
struct ec_params_switch_enable_backlight p;
char *e;
int rv;
if (argc != 2) {
fprintf(stderr, "Usage: %s <0|1>\n", argv[0]);
return -1;
}
p.enabled = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad value.\n");
return -1;
}
rv = ec_command(EC_CMD_SWITCH_ENABLE_BKLIGHT, 0,
&p, sizeof(p), NULL, 0);
if (rv < 0)
return rv;
printf("Success.\n");
return 0;
}
int cmd_ext_power_limit(int argc, char *argv[])
{
/* Version 1 is used, no support for obsolete version 0 */
struct ec_params_external_power_limit_v1 p;
char *e;
if (argc != 3) {
fprintf(stderr,
"Usage: %s <max_current_mA> <max_voltage_mV>\n",
argv[0]);
return -1;
}
p.current_lim = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad param1.\n");
return -1;
}
p.voltage_lim = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad param2.\n");
return -1;
}
/* Send version 1 of command */
return ec_command(EC_CMD_EXTERNAL_POWER_LIMIT, 1, &p, sizeof(p),
NULL, 0);
}
int cmd_charge_current_limit(int argc, char *argv[])
{
struct ec_params_current_limit p;
int rv;
char *e;
if (argc != 2) {
fprintf(stderr, "Usage: %s <max_current_mA>\n", argv[0]);
return -1;
}
p.limit = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad value.\n");
return -1;
}
rv = ec_command(EC_CMD_CHARGE_CURRENT_LIMIT, 0, &p, sizeof(p),
NULL, 0);
return rv;
}
int cmd_charge_control(int argc, char *argv[])
{
struct ec_params_charge_control p;
int rv;
if (argc != 2) {
fprintf(stderr, "Usage: %s <normal | idle | discharge>\n",
argv[0]);
return -1;
}
if (!strcasecmp(argv[1], "normal")) {
p.mode = CHARGE_CONTROL_NORMAL;
} else if (!strcasecmp(argv[1], "idle")) {
p.mode = CHARGE_CONTROL_IDLE;
} else if (!strcasecmp(argv[1], "discharge")) {
p.mode = CHARGE_CONTROL_DISCHARGE;
} else {
fprintf(stderr, "Bad value.\n");
return -1;
}
rv = ec_command(EC_CMD_CHARGE_CONTROL, 1, &p, sizeof(p), NULL, 0);
if (rv < 0) {
fprintf(stderr, "Is AC connected?\n");
return rv;
}
switch (p.mode) {
case CHARGE_CONTROL_NORMAL:
printf("Charge state machine normal mode.\n");
break;
case CHARGE_CONTROL_IDLE:
printf("Charge state machine force idle.\n");
break;
case CHARGE_CONTROL_DISCHARGE:
printf("Charge state machine force discharge.\n");
break;
default:
break;
}
return 0;
}
#define ST_CMD_SIZE ST_FLD_SIZE(ec_params_charge_state, cmd)
#define ST_PRM_SIZE(SUBCMD) \
(ST_CMD_SIZE + ST_FLD_SIZE(ec_params_charge_state, SUBCMD))
#define ST_RSP_SIZE(SUBCMD) ST_FLD_SIZE(ec_response_charge_state, SUBCMD)
/* Table of subcommand sizes for EC_CMD_CHARGE_STATE */
static const struct {
uint8_t to_ec_size;
uint8_t from_ec_size;
} cs_paramcount[] = {
/* Order must match enum charge_state_command */
{ ST_CMD_SIZE, ST_RSP_SIZE(get_state) },
{ ST_PRM_SIZE(get_param), ST_RSP_SIZE(get_param) },
{ ST_PRM_SIZE(set_param), 0},
};
BUILD_ASSERT(ARRAY_SIZE(cs_paramcount) == CHARGE_STATE_NUM_CMDS);
#undef ST_CMD_SIZE
#undef ST_PRM_SIZE
#undef ST_RSP_SIZE
static int cs_do_cmd(struct ec_params_charge_state *to_ec,
struct ec_response_charge_state *from_ec)
{
int rv;
int cmd = to_ec->cmd;
rv = ec_command(EC_CMD_CHARGE_STATE, 0,
to_ec, cs_paramcount[cmd].to_ec_size,
from_ec, cs_paramcount[cmd].from_ec_size);
return (rv < 0 ? 1 : 0);
}
static const char * const base_params[] = {
"chg_voltage",
"chg_current",
"chg_input_current",
"chg_status",
"chg_option",
"limit_power",
};
BUILD_ASSERT(ARRAY_SIZE(base_params) == CS_NUM_BASE_PARAMS);
static int cmd_charge_state(int argc, char **argv)
{
struct ec_params_charge_state param;
struct ec_response_charge_state resp;
uint32_t p, v;
int i, r;
char *e;
if (argc > 1 && !strcasecmp(argv[1], "show")) {
param.cmd = CHARGE_STATE_CMD_GET_STATE;
r = cs_do_cmd(¶m, &resp);
if (r)
return r;
printf("ac = %d\n", resp.get_state.ac);
printf("chg_voltage = %dmV\n", resp.get_state.chg_voltage);
printf("chg_current = %dmA\n", resp.get_state.chg_current);
printf("chg_input_current = %dmA\n",
resp.get_state.chg_input_current);
printf("batt_state_of_charge = %d%%\n",
resp.get_state.batt_state_of_charge);
return 0;
}
if (argc > 1 && !strcasecmp(argv[1], "param")) {
switch (argc) {
case 3:
if (!strcasecmp(argv[2], "help"))
break;
param.cmd = CHARGE_STATE_CMD_GET_PARAM;
p = strtoul(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad param: %s\n", argv[2]);
return -1;
}
param.get_param.param = p;
r = cs_do_cmd(¶m, &resp);
if (r)
return r;
v = resp.get_param.value;
if (p < CS_NUM_BASE_PARAMS)
printf("%d (0x%x) # %s\n", v, v,
base_params[p]);
else
printf("%d (0x%x)\n", v, v);
return 0;
case 4:
param.cmd = CHARGE_STATE_CMD_SET_PARAM;
p = strtoul(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad param: %s\n", argv[2]);
return -1;
}
v = strtoul(argv[3], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad value: %s\n", argv[3]);
return -1;
}
param.set_param.param = p;
param.set_param.value = v;
return cs_do_cmd(¶m, &resp);
}
printf("base params:\n");
for (i = 0; i < CS_NUM_BASE_PARAMS; i++)
printf(" %d %s\n", i, base_params[i]);
printf("custom profile params:\n");
printf(" 0x%x - 0x%x\n", CS_PARAM_CUSTOM_PROFILE_MIN,
CS_PARAM_CUSTOM_PROFILE_MAX);
return 0;
}
printf("Usage:\n");
printf(" %s show - show current state\n", argv[0]);
printf(" %s param NUM [VALUE] - get/set param NUM\n", argv[0]);
printf(" %s param help - show known param NUMs\n", argv[0]);
return 0;
}
int cmd_gpio_get(int argc, char *argv[])
{
struct ec_params_gpio_get_v1 p_v1;
struct ec_response_gpio_get_v1 r_v1;
int i, rv, subcmd, num_gpios;
int cmdver = 1;
if (!ec_cmd_version_supported(EC_CMD_GPIO_GET, cmdver)) {
struct ec_params_gpio_get p;
struct ec_response_gpio_get r;
/* Fall back to version 0 command */
cmdver = 0;
if (argc != 2) {
fprintf(stderr, "Usage: %s <GPIO name>\n", argv[0]);
return -1;
}
if (strlen(argv[1]) + 1 > sizeof(p.name)) {
fprintf(stderr, "GPIO name too long.\n");
return -1;
}
strcpy(p.name, argv[1]);
rv = ec_command(EC_CMD_GPIO_GET, cmdver, &p,
sizeof(p), &r, sizeof(r));
if (rv < 0)
return rv;
printf("GPIO %s = %d\n", p.name, r.val);
return 0;
}
if (argc > 2 || (argc == 2 && !strcmp(argv[1], "help"))) {
printf("Usage: %s [<subcmd> <GPIO name>]\n", argv[0]);
printf("'gpioget <GPIO_NAME>' - Get value by name\n");
printf("'gpioget count' - Get count of GPIOS\n");
printf("'gpioget all' - Get info for all GPIOs\n");
return -1;
}
/* Keeping it consistent with console command behavior */
if (argc == 1)
subcmd = EC_GPIO_GET_INFO;
else if (!strcmp(argv[1], "count"))
subcmd = EC_GPIO_GET_COUNT;
else if (!strcmp(argv[1], "all"))
subcmd = EC_GPIO_GET_INFO;
else
subcmd = EC_GPIO_GET_BY_NAME;
if (subcmd == EC_GPIO_GET_BY_NAME) {
p_v1.subcmd = EC_GPIO_GET_BY_NAME;
if (strlen(argv[1]) + 1 > sizeof(p_v1.get_value_by_name.name)) {
fprintf(stderr, "GPIO name too long.\n");
return -1;
}
strcpy(p_v1.get_value_by_name.name, argv[1]);
rv = ec_command(EC_CMD_GPIO_GET, cmdver, &p_v1,
sizeof(p_v1), &r_v1, sizeof(r_v1));
if (rv < 0)
return rv;
printf("GPIO %s = %d\n", p_v1.get_value_by_name.name,
r_v1.get_value_by_name.val);
return 0;
}
/* Need GPIO count for EC_GPIO_GET_COUNT or EC_GPIO_GET_INFO */
p_v1.subcmd = EC_GPIO_GET_COUNT;
rv = ec_command(EC_CMD_GPIO_GET, cmdver, &p_v1,
sizeof(p_v1), &r_v1, sizeof(r_v1));
if (rv < 0)
return rv;
if (subcmd == EC_GPIO_GET_COUNT) {
printf("GPIO COUNT = %d\n", r_v1.get_count.val);
return 0;
}
/* subcmd EC_GPIO_GET_INFO */
num_gpios = r_v1.get_count.val;
p_v1.subcmd = EC_GPIO_GET_INFO;
for (i = 0; i < num_gpios; i++) {
p_v1.get_info.index = i;
rv = ec_command(EC_CMD_GPIO_GET, cmdver, &p_v1,
sizeof(p_v1), &r_v1, sizeof(r_v1));
if (rv < 0)
return rv;
printf("%2d %-32s 0x%04X\n", r_v1.get_info.val,
r_v1.get_info.name, r_v1.get_info.flags);
}
return 0;
}
int cmd_gpio_set(int argc, char *argv[])
{
struct ec_params_gpio_set p;
char *e;
int rv;
if (argc != 3) {
fprintf(stderr, "Usage: %s <GPIO name> <0 | 1>\n", argv[0]);
return -1;
}
if (strlen(argv[1]) + 1 > sizeof(p.name)) {
fprintf(stderr, "GPIO name too long.\n");
return -1;
}
strcpy(p.name, argv[1]);
p.val = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad value.\n");
return -1;
}
rv = ec_command(EC_CMD_GPIO_SET, 0, &p, sizeof(p), NULL, 0);
if (rv < 0)
return rv;
printf("GPIO %s set to %d\n", p.name, p.val);
return 0;
}
void print_battery_flags(int flags)
{
printf(" Flags 0x%02x", flags);
if (flags & EC_BATT_FLAG_AC_PRESENT)
printf(" AC_PRESENT");
if (flags & EC_BATT_FLAG_BATT_PRESENT)
printf(" BATT_PRESENT");
if (flags & EC_BATT_FLAG_DISCHARGING)
printf(" DISCHARGING");
if (flags & EC_BATT_FLAG_CHARGING)
printf(" CHARGING");
if (flags & EC_BATT_FLAG_LEVEL_CRITICAL)
printf(" LEVEL_CRITICAL");
printf("\n");
}
int get_battery_command(int index)
{
struct ec_params_battery_static_info static_p;
struct ec_response_battery_static_info static_r;
struct ec_params_battery_dynamic_info dynamic_p;
struct ec_response_battery_dynamic_info dynamic_r;
int rv;
printf("Battery %d info:\n", index);
static_p.index = index;
rv = ec_command(EC_CMD_BATTERY_GET_STATIC, 0,
&static_p, sizeof(static_p),
&static_r, sizeof(static_r));
if (rv < 0)
return -1;
dynamic_p.index = index;
rv = ec_command(EC_CMD_BATTERY_GET_DYNAMIC, 0,
&dynamic_p, sizeof(dynamic_p),
&dynamic_r, sizeof(dynamic_r));
if (rv < 0)
return -1;
if (dynamic_r.flags & EC_BATT_FLAG_INVALID_DATA) {
printf(" Invalid data (not present?)\n");
return -1;
}
if (!is_string_printable(static_r.manufacturer))
goto cmd_error;
printf(" OEM name: %s\n", static_r.manufacturer);
if (!is_string_printable(static_r.model))
goto cmd_error;
printf(" Model number: %s\n", static_r.model);
if (!is_string_printable(static_r.type))
goto cmd_error;
printf(" Chemistry : %s\n", static_r.type);
if (!is_string_printable(static_r.serial))
goto cmd_error;
printf(" Serial number: %s\n", static_r.serial);
if (!is_battery_range(static_r.design_capacity))
goto cmd_error;
printf(" Design capacity: %u mAh\n", static_r.design_capacity);
if (!is_battery_range(dynamic_r.full_capacity))
goto cmd_error;
printf(" Last full charge: %u mAh\n", dynamic_r.full_capacity);
if (!is_battery_range(static_r.design_voltage))
goto cmd_error;
printf(" Design output voltage %u mV\n", static_r.design_voltage);
if (!is_battery_range(static_r.cycle_count))
goto cmd_error;
printf(" Cycle count %u\n", static_r.cycle_count);
if (!is_battery_range(dynamic_r.actual_voltage))
goto cmd_error;
printf(" Present voltage %u mV\n", dynamic_r.actual_voltage);
/* current can be negative */
printf(" Present current %d mA\n", dynamic_r.actual_current);
if (!is_battery_range(dynamic_r.remaining_capacity))
goto cmd_error;
printf(" Remaining capacity %u mAh\n",
dynamic_r.remaining_capacity);
if (!is_battery_range(dynamic_r.desired_voltage))
goto cmd_error;
printf(" Desired voltage %u mV\n", dynamic_r.desired_voltage);
if (!is_battery_range(dynamic_r.desired_current))
goto cmd_error;
printf(" Desired current %u mA\n", dynamic_r.desired_current);
print_battery_flags(dynamic_r.flags);
return 0;
cmd_error:
fprintf(stderr, "Bad battery info value.\n");
return -1;
}
int cmd_battery(int argc, char *argv[])
{
char batt_text[EC_MEMMAP_TEXT_MAX];
int rv, val;
char *e;
int index = 0;
if (argc > 2) {
fprintf(stderr, "Usage: %s [index]\n", argv[0]);
return -1;
} else if (argc == 2) {
index = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad battery index.\n");
return -1;
}
if (index > 0)
return get_battery_command(index);
}
/*
* TODO(b:65697620): When supported/required, read battery 0 information
* through EC commands as well.
*/
val = read_mapped_mem8(EC_MEMMAP_BATTERY_VERSION);
if (val < 1) {
fprintf(stderr, "Battery version %d is not supported\n", val);
return -1;
}
printf("Battery info:\n");
rv = read_mapped_string(EC_MEMMAP_BATT_MFGR, batt_text,
sizeof(batt_text));
if (rv < 0 || !is_string_printable(batt_text))
goto cmd_error;
printf(" OEM name: %s\n", batt_text);
rv = read_mapped_string(EC_MEMMAP_BATT_MODEL, batt_text,
sizeof(batt_text));
if (rv < 0 || !is_string_printable(batt_text))
goto cmd_error;
printf(" Model number: %s\n", batt_text);
rv = read_mapped_string(EC_MEMMAP_BATT_TYPE, batt_text,
sizeof(batt_text));
if (rv < 0 || !is_string_printable(batt_text))
goto cmd_error;
printf(" Chemistry : %s\n", batt_text);
rv = read_mapped_string(EC_MEMMAP_BATT_SERIAL, batt_text,
sizeof(batt_text));
printf(" Serial number: %s\n", batt_text);
val = read_mapped_mem32(EC_MEMMAP_BATT_DCAP);
if (!is_battery_range(val))
goto cmd_error;
printf(" Design capacity: %u mAh\n", val);
val = read_mapped_mem32(EC_MEMMAP_BATT_LFCC);
if (!is_battery_range(val))
goto cmd_error;
printf(" Last full charge: %u mAh\n", val);
val = read_mapped_mem32(EC_MEMMAP_BATT_DVLT);
if (!is_battery_range(val))
goto cmd_error;
printf(" Design output voltage %u mV\n", val);
val = read_mapped_mem32(EC_MEMMAP_BATT_CCNT);
if (!is_battery_range(val))
goto cmd_error;
printf(" Cycle count %u\n", val);
val = read_mapped_mem32(EC_MEMMAP_BATT_VOLT);
if (!is_battery_range(val))
goto cmd_error;
printf(" Present voltage %u mV\n", val);
val = read_mapped_mem32(EC_MEMMAP_BATT_RATE);
if (!is_battery_range(val))
goto cmd_error;
printf(" Present current %u mA\n", val);
val = read_mapped_mem32(EC_MEMMAP_BATT_CAP);
if (!is_battery_range(val))
goto cmd_error;
printf(" Remaining capacity %u mAh\n", val);
val = read_mapped_mem8(EC_MEMMAP_BATT_FLAG);
print_battery_flags(val);
return 0;
cmd_error:
fprintf(stderr, "Bad battery info value. Check protocol version.\n");
return -1;
}
int cmd_battery_cut_off(int argc, char *argv[])
{
struct ec_params_battery_cutoff p;
int cmd_version;
int rv;
memset(&p, 0, sizeof(p));
if (ec_cmd_version_supported(EC_CMD_BATTERY_CUT_OFF, 1)) {
cmd_version = 1;
if (argc > 1) {
if (!strcasecmp(argv[1], "at-shutdown")) {
p.flags = EC_BATTERY_CUTOFF_FLAG_AT_SHUTDOWN;
} else {
fprintf(stderr, "Bad parameter: %s\n", argv[1]);
return -1;
}
}
} else {
/* Fall back to version 0 command */
cmd_version = 0;
if (argc > 1) {
if (!strcasecmp(argv[1], "at-shutdown")) {
fprintf(stderr, "Explicit 'at-shutdown' ");
fprintf(stderr, "parameter not supported.\n");
} else {
fprintf(stderr, "Bad parameter: %s\n", argv[1]);
}
return -1;
}
}
rv = ec_command(EC_CMD_BATTERY_CUT_OFF, cmd_version, &p, sizeof(p),
NULL, 0);
rv = (rv < 0 ? rv : 0);
if (rv < 0) {
fprintf(stderr, "Failed to cut off battery, rv=%d\n", rv);
fprintf(stderr, "It is expected if the rv is -%d "
"(EC_RES_INVALID_COMMAND) if the battery "
"doesn't support cut-off function.\n",
EC_RES_INVALID_COMMAND);
} else {
printf("\n");
printf("SUCCESS. The battery has arranged a cut-off.\n");
if (cmd_version == 1 &&
(p.flags & EC_BATTERY_CUTOFF_FLAG_AT_SHUTDOWN))
printf("The battery will be cut off after shutdown.\n");
else
printf("The system should be shutdown immediately.\n");
printf("\n");
}
return rv;
}
int cmd_battery_vendor_param(int argc, char *argv[])
{
struct ec_params_battery_vendor_param p;
struct ec_response_battery_vendor_param r;
char *e;
int rv;
if (argc < 3)
goto cmd_battery_vendor_param_usage;
if (!strcasecmp(argv[1], "get"))
p.mode = BATTERY_VENDOR_PARAM_MODE_GET;
else if (!strcasecmp(argv[1], "set"))
p.mode = BATTERY_VENDOR_PARAM_MODE_SET;
else
goto cmd_battery_vendor_param_usage;
p.param = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Invalid param.\n");
goto cmd_battery_vendor_param_usage;
}
if (p.mode == BATTERY_VENDOR_PARAM_MODE_SET) {
if (argc != 4) {
fprintf(stderr, "Missing value.\n");
goto cmd_battery_vendor_param_usage;
}
p.value = strtol(argv[3], &e, 0);
if (e && *e) {
fprintf(stderr, "Invalid value.\n");
goto cmd_battery_vendor_param_usage;
}
}
rv = ec_command(EC_CMD_BATTERY_VENDOR_PARAM, 0, &p, sizeof(p),
&r, sizeof(r));
if (rv < 0)
return rv;
printf("0x%08x\n", r.value);
return 0;
cmd_battery_vendor_param_usage:
fprintf(stderr,
"Usage:\t %s get <param>\n"
"\t %s set <param> <value>\n",
argv[0], argv[0]);
return -1;
}
int cmd_board_version(int argc, char *argv[])
{
struct ec_response_board_version response;
int rv;
rv = ec_command(EC_CMD_GET_BOARD_VERSION, 0, NULL, 0, &response,
sizeof(response));
if (rv < 0)
return rv;
printf("%d\n", response.board_version);
return rv;
}
static void cmd_cbi_help(char *cmd)
{
fprintf(stderr,
" Usage: %s get <tag> [get_flag]\n"
" Usage: %s set <tag> <value/string> <size> [set_flag]\n"
" <tag> is one of:\n"
" 0: BOARD_VERSION\n"
" 1: OEM_ID\n"
" 2: SKU_ID\n"
" 3: DRAM_PART_NUM (string)\n"
" 4: OEM_NAME (string)\n"
" 5: MODEL_ID\n"
" <size> is the size of the data in byte. It should be zero for\n"
" string types.\n"
" <value/string> is an integer or a string to be set\n"
" [get_flag] is combination of:\n"
" 01b: Invalidate cache and reload data from EEPROM\n"
" [set_flag] is combination of:\n"
" 01b: Skip write to EEPROM. Use for back-to-back writes\n"
" 10b: Set all fields to defaults first\n", cmd, cmd);
}
static int cmd_cbi_is_string_field(enum cbi_data_tag tag)
{
return tag == CBI_TAG_DRAM_PART_NUM || tag == CBI_TAG_OEM_NAME;
}
/*
* Write value to CBI
*
* TODO: Support asynchronous write
*/
static int cmd_cbi(int argc, char *argv[])
{
enum cbi_data_tag tag;
char *e;
int rv;
if (argc < 3) {
fprintf(stderr, "Invalid number of params\n");
cmd_cbi_help(argv[0]);
return -1;
}
/* Tag */
tag = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad tag\n");
return -1;
}
if (!strcasecmp(argv[1], "get")) {
struct ec_params_get_cbi p = { 0 };
uint8_t *r;
int i;
p.tag = tag;
if (argc > 3) {
p.flag = strtol(argv[3], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad flag\n");
return -1;
}
}
rv = ec_command(EC_CMD_GET_CROS_BOARD_INFO, 0, &p, sizeof(p),
ec_inbuf, ec_max_insize);
if (rv < 0) {
fprintf(stderr, "Error code: %d\n", rv);
return rv;
}
if (rv < sizeof(uint8_t)) {
fprintf(stderr, "Invalid size: %d\n", rv);
return -1;
}
r = ec_inbuf;
if (cmd_cbi_is_string_field(tag)) {
printf("%.*s", rv, (const char *)r);
} else {
if (rv <= sizeof(uint32_t))
printf("As integer: %u (0x%x)\n", r[0], r[0]);
printf("As binary:");
for (i = 0; i < rv; i++) {
if (i % 32 == 31)
printf("\n");
printf(" %02x", r[i]);
}
}
printf("\n");
return 0;
} else if (!strcasecmp(argv[1], "set")) {
struct ec_params_set_cbi *p =
(struct ec_params_set_cbi *)ec_outbuf;
void *val_ptr;
uint32_t val;
uint8_t size;
if (argc < 5) {
fprintf(stderr, "Invalid number of params\n");
cmd_cbi_help(argv[0]);
return -1;
}
memset(p, 0, ec_max_outsize);
p->tag = tag;
if (cmd_cbi_is_string_field(tag)) {
val_ptr = argv[3];
size = strlen(val_ptr) + 1;
} else {
val = strtol(argv[3], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad value\n");
return -1;
}
size = strtol(argv[4], &e, 0);
if ((e && *e) || size < 1 || 4 < size ||
val >= (1ull << size*8)) {
fprintf(stderr, "Bad size: %d\n", size);
return -1;
}
val_ptr = &val;
}
if (size > ec_max_outsize - sizeof(*p)) {
fprintf(stderr, "Size exceeds parameter buffer: %d\n",
size);
return -1;
}
/* Little endian */
memcpy(p->data, val_ptr, size);
p->size = size;
if (argc > 5) {
p->flag = strtol(argv[5], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad flag\n");
return -1;
}
}
rv = ec_command(EC_CMD_SET_CROS_BOARD_INFO, 0,
p, sizeof(*p) + size, NULL, 0);
if (rv < 0) {
if (rv == -EC_RES_ACCESS_DENIED - EECRESULT)
fprintf(stderr, "Write-protect is enabled or "
"EC explicitly refused to change the "
"requested field.\n");
else
fprintf(stderr, "Error code: %d\n", rv);
return rv;
}
return 0;
}
fprintf(stderr, "Invalid sub command: %s\n", argv[1]);
cmd_cbi_help(argv[0]);
return -1;
}
int cmd_chipinfo(int argc, char *argv[])
{
struct ec_response_get_chip_info info;
int rv;
printf("Chip info:\n");
rv = ec_command(EC_CMD_GET_CHIP_INFO, 0, NULL, 0, &info, sizeof(info));
if (rv < 0)
return rv;
printf(" vendor: %s\n", info.vendor);
printf(" name: %s\n", info.name);
printf(" revision: %s\n", info.revision);
return 0;
}
int cmd_proto_info(int argc, char *argv[])
{
struct ec_response_get_protocol_info info;
int rv;
int i;
printf("Protocol info:\n");
rv = ec_command(EC_CMD_GET_PROTOCOL_INFO, 0, NULL, 0,
&info, sizeof(info));
if (rv < 0) {
fprintf(stderr, "Protocol info unavailable. EC probably only "
"supports protocol version 2.\n");
return rv;
}
printf(" protocol versions:");
for (i = 0; i < 32; i++) {
if (info.protocol_versions & BIT(i))
printf(" %d", i);
}
printf("\n");
printf(" max request: %4d bytes\n", info.max_request_packet_size);
printf(" max response: %4d bytes\n", info.max_response_packet_size);
printf(" flags: 0x%08x\n", info.flags);
if (info.flags & EC_PROTOCOL_INFO_IN_PROGRESS_SUPPORTED)
printf(" EC_RES_IN_PROGRESS supported\n");
return 0;
}
static int ec_hash_help(const char *cmd)
{
printf("Usage:\n");
printf(" %s - get last hash\n", cmd);
printf(" %s abort - abort hashing\n", cmd);
printf(" %s start [<offset> <size> [<nonce>]] - start hashing\n", cmd);
printf(" %s recalc [<offset> <size> [<nonce>]] - sync rehash\n", cmd);
printf("\n"
"If <offset> is RO or RW, offset and size are computed\n"
"automatically for the EC-RO or EC-RW firmware image.\n");
return 0;
}
static int ec_hash_print(const struct ec_response_vboot_hash *r)
{
int i;
if (r->status == EC_VBOOT_HASH_STATUS_BUSY) {
printf("status: busy\n");
return 0;
} else if (r->status == EC_VBOOT_HASH_STATUS_NONE) {
printf("status: unavailable\n");
return 0;
} else if (r->status != EC_VBOOT_HASH_STATUS_DONE) {
printf("status: %d\n", r->status);
return 0;
}
printf("status: done\n");
if (r->hash_type == EC_VBOOT_HASH_TYPE_SHA256)
printf("type: SHA-256\n");
else
printf("type: %d\n", r->hash_type);
printf("offset: 0x%08x\n", r->offset);
printf("size: 0x%08x\n", r->size);
printf("hash: ");
for (i = 0; i < r->digest_size; i++)
printf("%02x", r->hash_digest[i]);
printf("\n");
return 0;
}
int cmd_ec_hash(int argc, char *argv[])
{
struct ec_params_vboot_hash p;
struct ec_response_vboot_hash r;
char *e;
int rv;
memset(&p, 0, sizeof(p));
if (argc < 2) {
/* Get hash status */
p.cmd = EC_VBOOT_HASH_GET;
rv = ec_command(EC_CMD_VBOOT_HASH, 0,
&p, sizeof(p), &r, sizeof(r));
if (rv < 0)
return rv;
return ec_hash_print(&r);
}
if (argc == 2 && !strcasecmp(argv[1], "abort")) {
/* Abort hash calculation */
p.cmd = EC_VBOOT_HASH_ABORT;
rv = ec_command(EC_CMD_VBOOT_HASH, 0,
&p, sizeof(p), &r, sizeof(r));
return (rv < 0 ? rv : 0);
}
/* The only other commands are start and recalc */
if (!strcasecmp(argv[1], "start"))
p.cmd = EC_VBOOT_HASH_START;
else if (!strcasecmp(argv[1], "recalc"))
p.cmd = EC_VBOOT_HASH_RECALC;
else
return ec_hash_help(argv[0]);
p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
if (argc < 3) {
fprintf(stderr, "Must specify offset\n");
return -1;
}
if (!strcasecmp(argv[2], "ro")) {
p.offset = EC_VBOOT_HASH_OFFSET_RO;
p.size = 0;
printf("Hashing EC-RO...\n");
} else if (!strcasecmp(argv[2], "rw")) {
p.offset = EC_VBOOT_HASH_OFFSET_ACTIVE;
p.size = 0;
printf("Hashing EC-RW...\n");
} else if (argc < 4) {
fprintf(stderr, "Must specify size\n");
return -1;
} else {
p.offset = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad offset.\n");
return -1;
}
p.size = strtol(argv[3], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad size.\n");
return -1;
}
printf("Hashing %d bytes at offset %d...\n", p.size, p.offset);
}
if (argc == 5) {
/*
* Technically nonce can be any binary data up to 64 bytes,
* but this command only supports a 32-bit value.
*/
uint32_t nonce = strtol(argv[4], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad nonce integer.\n");
return -1;
}
memcpy(p.nonce_data, &nonce, sizeof(nonce));
p.nonce_size = sizeof(nonce);
} else
p.nonce_size = 0;
rv = ec_command(EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), &r, sizeof(r));
if (rv < 0)
return rv;
/* Start command doesn't wait for hashing to finish */
if (p.cmd == EC_VBOOT_HASH_START)
return 0;
/* Recalc command does wait around, so a result is ready now */
return ec_hash_print(&r);
}
int cmd_rtc_get(int argc, char *argv[])
{
struct ec_response_rtc r;
int rv;
rv = ec_command(EC_CMD_RTC_GET_VALUE, 0, NULL, 0, &r, sizeof(r));
if (rv < 0)
return rv;
printf("Current time: 0x%08x (%d)\n", r.time, r.time);
return 0;
}
int cmd_rtc_set(int argc, char *argv[])
{
struct ec_params_rtc p;
char *e;
int rv;
if (argc != 2) {
fprintf(stderr, "Usage: %s <time>\n", argv[0]);
return -1;
}
p.time = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad time.\n");
return -1;
}
rv = ec_command(EC_CMD_RTC_SET_VALUE, 0, &p, sizeof(p), NULL, 0);
if (rv < 0)
return rv;
printf("Time set.\n");
return 0;
}
int cmd_rtc_set_alarm(int argc, char *argv[])
{
struct ec_params_rtc p;
char *e;
int rv;
if (argc != 2) {
fprintf(stderr, "Usage: %s <sec>\n", argv[0]);
return -1;
}
p.time = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad time.\n");
return -1;
}
rv = ec_command(EC_CMD_RTC_SET_ALARM, 0, &p, sizeof(p), NULL, 0);
if (rv < 0)
return rv;
if (p.time == 0)
printf("Disabling alarm.\n");
else
printf("Alarm set to go off in %d secs.\n", p.time);
return 0;
}
int cmd_rtc_get_alarm(int argc, char *argv[])
{
struct ec_response_rtc r;
int rv;
rv = ec_command(EC_CMD_RTC_GET_ALARM, 0, NULL, 0, &r, sizeof(r));
if (rv < 0)
return rv;
if (r.time == 0)
printf("Alarm not set\n");
else
printf("Alarm to go off in %d secs\n", r.time);
return 0;
}
int cmd_console(int argc, char *argv[])
{
char *out = (char *)ec_inbuf;
int rv;
/* Snapshot the EC console */
rv = ec_command(EC_CMD_CONSOLE_SNAPSHOT, 0, NULL, 0, NULL, 0);
if (rv < 0)
return rv;
/* Loop and read from the snapshot until it's done */
while (1) {
rv = ec_command(EC_CMD_CONSOLE_READ, 0,
NULL, 0, ec_inbuf, ec_max_insize);
if (rv < 0)
return rv;
/* Empty response means done */
if (!rv || !*out)
break;
/* Make sure output is null-terminated, then dump it */
out[ec_max_insize - 1] = '\0';
fputs(out, stdout);
}
printf("\n");
return 0;
}
struct param_info {
const char *name; /* name of this parameter */
const char *help; /* help message */
int size; /* size in bytes */
int offset; /* offset within structure */
};
#define FIELD(fname, field, help_str) \
{ \
.name = fname, \
.help = help_str, \
.size = sizeof(((struct ec_mkbp_config *)NULL)->field), \
.offset = __builtin_offsetof(struct ec_mkbp_config, field), \
}
static const struct param_info keyconfig_params[] = {
FIELD("scan_period", scan_period_us, "period between scans"),
FIELD("poll_timeout", poll_timeout_us,
"revert to irq mode after no activity for this long"),
FIELD("min_post_scan_delay", min_post_scan_delay_us,
"minimum post-scan delay before starting a new scan"),
FIELD("output_settle", output_settle_us,
"delay to wait for output to settle"),
FIELD("debounce_down", debounce_down_us,
"time for debounce on key down"),
FIELD("debounce_up", debounce_up_us, "time for debounce on key up"),
FIELD("fifo_max_depth", fifo_max_depth,
"maximum depth to allow for fifo (0 = disable)"),
FIELD("flags", flags, "0 to disable scanning, 1 to enable"),
};
static const struct param_info *find_field(const struct param_info *params,
int count, const char *name, unsigned int *nump)
{
const struct param_info *param;
int i;
for (i = 0, param = params; i < count; i++, param++) {
if (0 == strcmp(param->name, name)) {
if (nump)
*nump = i;
return param;
}
}
fprintf(stderr, "Unknown parameter '%s'\n", name);
return NULL;
}
static int get_value(const struct param_info *param, const char *config)
{
const char *field;
field = config + param->offset;
switch (param->size) {
case 1:
return *(uint8_t *)field;
case 2:
return *(uint16_t *)field;
case 4:
return *(uint32_t *)field;
default:
fprintf(stderr, "Internal error: unknown size %d\n",
param->size);
}
return -1;
}
static int show_fields(struct ec_mkbp_config *config, int argc, char *argv[])
{
const struct param_info *param;
uint32_t mask;
int i;
if (!argc) {
mask = -1U; /* show all fields */
} else {
mask = 0;
while (argc > 0) {
unsigned int num;
param = find_field(keyconfig_params,
ARRAY_SIZE(keyconfig_params),
argv[0], &num);
if (!param)
return -1;
mask |= 1 << num;
argc--;
argv++;
}
}
param = keyconfig_params;
for (i = 0; i < ARRAY_SIZE(keyconfig_params); i++, param++) {
if (mask & BIT(i)) {
fprintf(stderr, "%-12s %u\n", param->name,
get_value(param, (char *)config));
}
}
return 0;
}
static int cmd_kbinfo(int argc, char *argv[])
{
struct ec_params_mkbp_info info = {
.info_type = EC_MKBP_INFO_KBD,
};
struct ec_response_mkbp_info resp;
int rv;
if (argc > 1) {
fprintf(stderr, "Too many args\n");
return -1;
}
rv = ec_command(EC_CMD_MKBP_INFO, 0, &info, sizeof(info), &resp,
sizeof(resp));
if (rv < 0)
return rv;
printf("Matrix rows: %d\n", resp.rows);
printf("Matrix columns: %d\n", resp.cols);
return 0;
}
static int cmd_kbid(int argc, char *argv[])
{
struct ec_response_keyboard_id response;
int rv;
if (argc > 1) {
fprintf(stderr, "Too many args\n");
return -1;
}
rv = ec_command(EC_CMD_GET_KEYBOARD_ID, 0, NULL, 0, &response,
sizeof(response));
if (rv < 0)
return rv;
switch (response.keyboard_id) {
case KEYBOARD_ID_UNSUPPORTED:
/* Keyboard ID was not supported */
printf("Keyboard doesn't support ID\n");
break;
case KEYBOARD_ID_UNREADABLE:
/* Ghosting ID was detected */
printf("Reboot and keep hands off the keyboard during"
" next boot-up\n");
break;
default:
/* Valid keyboard ID value was reported*/
printf("%x\n", response.keyboard_id);
}
return rv;
}
static int cmd_keyconfig(int argc, char *argv[])
{
struct ec_params_mkbp_set_config req;
int cmd;
int rv;
if (argc < 2) {
const struct param_info *param;
int i;
fprintf(stderr, "Usage: %s get [<param>] - print params\n"
"\t%s set [<param>> <value>]\n"
" Available params are: (all time values are in us)",
argv[0], argv[0]);
param = keyconfig_params;
for (i = 0; i < ARRAY_SIZE(keyconfig_params); i++, param++) {
fprintf(stderr, "%-12s %s\n", param->name,
param->name);
}
return -1;
}
/* Get the command */
if (0 == strcmp(argv[1], "get")) {
cmd = EC_CMD_MKBP_GET_CONFIG;
} else if (0 == strcmp(argv[1], "set")) {
cmd = EC_CMD_MKBP_SET_CONFIG;
} else {
fprintf(stderr, "Invalid command '%s\n", argv[1]);
return -1;
}
switch (cmd) {
case EC_CMD_MKBP_GET_CONFIG:
/* Read the existing config */
rv = ec_command(cmd, 0, NULL, 0, &req, sizeof(req));
if (rv < 0)
return rv;
show_fields(&req.config, argc - 2, argv + 2);
break;
}
return 0;
}
/* Index is already checked. argv[0] is first param value */
static int cmd_tmp006cal_v0(int idx, int argc, char *argv[])
{
struct ec_params_tmp006_get_calibration pg;
struct ec_response_tmp006_get_calibration_v0 rg;
struct ec_params_tmp006_set_calibration_v0 ps;
float val;
char *e;
int i, rv;
/* Get current values */
pg.index = idx;
rv = ec_command(EC_CMD_TMP006_GET_CALIBRATION, 0,
&pg, sizeof(pg), &rg, sizeof(rg));
if (rv < 0)
return rv;
if (!argc) {
/* If no new values are given, just print what we have */
printf("S0: %e\n", rg.s0);
printf("b0: %e\n", rg.b0);
printf("b1: %e\n", rg.b1);
printf("b2: %e\n", rg.b2);
return EC_SUCCESS;
}
/* Prepare to reuse the current values */
memset(&ps, 0, sizeof(ps));
ps.index = idx;
ps.s0 = rg.s0;
ps.b0 = rg.b0;
ps.b1 = rg.b1;
ps.b2 = rg.b2;
/* Parse up to four args, skipping any that are just "-" */
for (i = 0; i < argc && i < 4; i++) {
if (!strcmp(argv[i], "-"))
continue;
val = strtod(argv[i], &e);
if (e && *e) {
fprintf(stderr,
"Bad arg \"%s\". Use \"-\" to skip a param.\n",
argv[i]);
return -1;
}
switch (i) {
case 0:
ps.s0 = val;
break;
case 1:
ps.b0 = val;
break;
case 2:
ps.b1 = val;
break;
case 3:
ps.b2 = val;
break;
}
}
/* Set 'em */
return ec_command(EC_CMD_TMP006_SET_CALIBRATION, 0,
&ps, sizeof(ps), NULL, 0);
}
/* Index is already checked. argv[0] is first param value */
static int cmd_tmp006cal_v1(int idx, int argc, char *argv[])
{
struct ec_params_tmp006_get_calibration pg;
struct ec_response_tmp006_get_calibration_v1 *rg = ec_inbuf;
struct ec_params_tmp006_set_calibration_v1 *ps = ec_outbuf;
float val;
char *e;
int i, rv, cmdsize;
/* Algorithm 1 parameter names */
static const char * const alg1_pname[] = {
"s0", "a1", "a2", "b0", "b1", "b2", "c2",
"d0", "d1", "ds", "e0", "e1",
};
/* Get current values */
pg.index = idx;
rv = ec_command(EC_CMD_TMP006_GET_CALIBRATION, 1,
&pg, sizeof(pg), rg, ec_max_insize);
if (rv < 0)
return rv;
if (!argc) {
/* If no new values are given, just print what we have */
printf("algorithm: %d\n", rg->algorithm);
printf("params:\n");
/* We only know about alg 1 at the moment */
if (rg->algorithm == 1)
for (i = 0; i < rg->num_params; i++)
printf(" %s %e\n", alg1_pname[i], rg->val[i]);
else
for (i = 0; i < rg->num_params; i++)
printf(" param%d %e\n", i, rg->val[i]);
return EC_SUCCESS;
}
/* Prepare to reuse the current values */
memset(ps, 0, ec_max_outsize);
ps->index = idx;
ps->algorithm = rg->algorithm;
ps->num_params = rg->num_params;
for (i = 0; i < rg->num_params; i++)
ps->val[i] = rg->val[i];
/* Parse the args, skipping any that are just "-" */
for (i = 0; i < argc && i < rg->num_params; i++) {
if (!strcmp(argv[i], "-"))
continue;
val = strtod(argv[i], &e);
if (e && *e) {
fprintf(stderr,
"Bad arg \"%s\". Use \"-\" to skip a param.\n",
argv[i]);
return -1;
}
ps->val[i] = val;
}
/* Set 'em */
cmdsize = sizeof(*ps) + ps->num_params * sizeof(ps->val[0]);
return ec_command(EC_CMD_TMP006_SET_CALIBRATION, 1,
ps, cmdsize, NULL, 0);
}
int cmd_tmp006cal(int argc, char *argv[])
{
char *e;
int idx;
if (argc < 2) {
fprintf(stderr, "Must specify tmp006 index.\n");
return -1;
}
idx = strtol(argv[1], &e, 0);
if ((e && *e) || idx < 0 || idx > 255) {
fprintf(stderr, "Bad index.\n");
return -1;
}
/* Pass just the params (if any) to the helper function */
argc -= 2;
argv += 2;
if (ec_cmd_version_supported(EC_CMD_TMP006_GET_CALIBRATION, 1))
return cmd_tmp006cal_v1(idx, argc, argv);
if (ec_cmd_version_supported(EC_CMD_TMP006_GET_CALIBRATION, 0))
return cmd_tmp006cal_v0(idx, argc, argv);
printf("The EC is being stupid\n");
return -1;
}
int cmd_tmp006raw(int argc, char *argv[])
{
struct ec_params_tmp006_get_raw p;
struct ec_response_tmp006_get_raw r;
char *e;
int idx;
int rv;
if (argc != 2) {
fprintf(stderr, "Must specify tmp006 index.\n");
return -1;
}
idx = strtol(argv[1], &e, 0);
if ((e && *e) || idx < 0 || idx > 255) {
fprintf(stderr, "Bad index.\n");
return -1;
}
p.index = idx;
rv = ec_command(EC_CMD_TMP006_GET_RAW, 0, &p, sizeof(p), &r, sizeof(r));
if (rv < 0)
return rv;
printf("T: %d.%02d K\n", r.t / 100, r.t % 100);
printf("V: %d nV\n", r.v);
return EC_SUCCESS;
}
static int cmd_hang_detect(int argc, char *argv[])
{
struct ec_params_hang_detect req;
char *e;
memset(&req, 0, sizeof(req));
if (argc == 2 && !strcasecmp(argv[1], "stop")) {
req.flags = EC_HANG_STOP_NOW;
return ec_command(EC_CMD_HANG_DETECT, 0, &req, sizeof(req),
NULL, 0);
}
if (argc == 2 && !strcasecmp(argv[1], "start")) {
req.flags = EC_HANG_START_NOW;
return ec_command(EC_CMD_HANG_DETECT, 0, &req, sizeof(req),
NULL, 0);
}
if (argc == 4) {
req.flags = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad flags.\n");
return -1;
}
req.host_event_timeout_msec = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad event timeout.\n");
return -1;
}
req.warm_reboot_timeout_msec = strtol(argv[3], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad reboot timeout.\n");
return -1;
}
printf("hang flags=0x%x\n"
"event_timeout=%d ms\n"
"reboot_timeout=%d ms\n",
req.flags, req.host_event_timeout_msec,
req.warm_reboot_timeout_msec);
return ec_command(EC_CMD_HANG_DETECT, 0, &req, sizeof(req),
NULL, 0);
}
fprintf(stderr,
"Must specify start/stop or <flags> <event_ms> <reboot_ms>\n");
return -1;
}
enum port_80_event {
PORT_80_EVENT_RESUME = 0x1001, /* S3->S0 transition */
PORT_80_EVENT_RESET = 0x1002, /* RESET transition */
};
int cmd_port80_read(int argc, char *argv[])
{
struct ec_params_port80_read p;
int cmdver = 1, rv;
int i, head, tail;
uint16_t *history;
uint32_t writes, history_size;
struct ec_response_port80_read rsp;
int printed = 0;
if (!ec_cmd_version_supported(EC_CMD_PORT80_READ, cmdver)) {
/* fall back to last boot */
struct ec_response_port80_last_boot r;
rv = ec_command(EC_CMD_PORT80_LAST_BOOT, 0,
NULL, 0, &r, sizeof(r));
fprintf(stderr, "Last boot %2x\n", r.code);
printf("done.\n");
return 0;
}
/* read writes and history_size */
p.subcmd = EC_PORT80_GET_INFO;
rv = ec_command(EC_CMD_PORT80_READ, cmdver,
&p, sizeof(p), &rsp, sizeof(rsp));
if (rv < 0) {
fprintf(stderr, "Read error at writes\n");
return rv;
}
writes = rsp.get_info.writes;
history_size = rsp.get_info.history_size;
history = malloc(history_size*sizeof(uint16_t));
if (!history) {
fprintf(stderr, "Unable to allocate buffer.\n");
return -1;
}
/* As the history buffer is quite large, we read data in chunks, with
size in bytes of EC_PORT80_SIZE_MAX in each chunk.
Incrementing offset until all history buffer has been read. To
simplify the design, chose HISTORY_LEN is always multiple of
EC_PORT80_SIZE_MAX.
offset: entry offset from the beginning of history buffer.
num_entries: number of entries requested.
*/
p.subcmd = EC_PORT80_READ_BUFFER;
for (i = 0; i < history_size; i += EC_PORT80_SIZE_MAX) {
p.read_buffer.offset = i;
p.read_buffer.num_entries = EC_PORT80_SIZE_MAX;
rv = ec_command(EC_CMD_PORT80_READ, cmdver,
&p, sizeof(p), &rsp, sizeof(rsp));
if (rv < 0) {
fprintf(stderr, "Read error at offset %d\n", i);
free(history);
return rv;
}
memcpy((void *)(history + i), rsp.data.codes,
EC_PORT80_SIZE_MAX*sizeof(uint16_t));
}
head = writes;
if (head > history_size)
tail = head - history_size;
else
tail = 0;
fprintf(stderr, "Port 80 writes");
for (i = tail; i < head; i++) {
int e = history[i % history_size];
switch (e) {
case PORT_80_EVENT_RESUME:
fprintf(stderr, "\n(S3->S0)");
printed = 0;
break;
case PORT_80_EVENT_RESET:
fprintf(stderr, "\n(RESET)");
printed = 0;
break;
default:
if (!(printed++ % 20))
fprintf(stderr, "\n ");
fprintf(stderr, " %02x", e);
}
}
fprintf(stderr, " <--new\n");
free(history);
printf("done.\n");
return 0;
}
struct command {
const char *name;
int (*handler)(int argc, char *argv[]);
};
int cmd_force_lid_open(int argc, char *argv[])
{
struct ec_params_force_lid_open p;
char *e;
int rv;
if (argc != 2) {
fprintf(stderr, "Usage: %s <0|1>\n", argv[0]);
return -1;
}
p.enabled = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad value.\n");
return -1;
}
rv = ec_command(EC_CMD_FORCE_LID_OPEN, 0, &p, sizeof(p), NULL, 0);
if (rv < 0)
return rv;
printf("Success.\n");
return 0;
}
int cmd_charge_port_override(int argc, char *argv[])
{
struct ec_params_charge_port_override p;
char *e;
int rv;
if (argc < 2) {
fprintf(stderr, "Usage: %s <port# | dontcharge | off>\n",
argv[0]);
return -1;
}
if (!strcasecmp(argv[1], "dontcharge"))
p.override_port = OVERRIDE_DONT_CHARGE;
else if (!strcasecmp(argv[1], "off"))
p.override_port = OVERRIDE_OFF;
else {
p.override_port = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad parameter.\n");
return -1;
}
}
rv = ec_command(EC_CMD_PD_CHARGE_PORT_OVERRIDE, 0, &p, sizeof(p),
NULL, 0);
if (rv < 0)
return rv;
printf("Override port set to %d\n", p.override_port);
return 0;
}
int cmd_pd_log(int argc, char *argv[])
{
union {
struct ec_response_pd_log r;
uint32_t words[8]; /* space for the payload */
} u;
struct mcdp_info minfo;
struct ec_response_usb_pd_power_info pinfo;
int rv;
unsigned long long milliseconds;
unsigned seconds;
time_t now;
struct tm ltime;
char time_str[64];
while (1) {
now = time(NULL);
rv = ec_command(EC_CMD_PD_GET_LOG_ENTRY, 0,
NULL, 0, &u, sizeof(u));
if (rv < 0)
return rv;
if (u.r.type == PD_EVENT_NO_ENTRY) {
printf("--- END OF LOG ---\n");
break;
}
/* the timestamp is in 1024th of seconds */
milliseconds = ((uint64_t)u.r.timestamp <<
PD_LOG_TIMESTAMP_SHIFT) / 1000;
/* the timestamp is the number of milliseconds in the past */
seconds = (milliseconds + 999) / 1000;
milliseconds -= seconds * 1000;
now -= seconds;
localtime_r(&now, <ime);
strftime(time_str, sizeof(time_str), "%F %T", <ime);
printf("%s.%03lld P%d ", time_str, -milliseconds,
PD_LOG_PORT(u.r.size_port));
if (u.r.type == PD_EVENT_MCU_CHARGE) {
if (u.r.data & CHARGE_FLAGS_OVERRIDE)
printf("override ");
if (u.r.data & CHARGE_FLAGS_DELAYED_OVERRIDE)
printf("pending_override ");
memcpy(&pinfo.meas, u.r.payload,
sizeof(struct usb_chg_measures));
pinfo.dualrole = !!(u.r.data & CHARGE_FLAGS_DUAL_ROLE);
pinfo.role = u.r.data & CHARGE_FLAGS_ROLE_MASK;
pinfo.type = (u.r.data & CHARGE_FLAGS_TYPE_MASK)
>> CHARGE_FLAGS_TYPE_SHIFT;
pinfo.max_power = 0;
print_pd_power_info(&pinfo);
} else if (u.r.type == PD_EVENT_MCU_CONNECT) {
printf("New connection\n");
} else if (u.r.type == PD_EVENT_MCU_BOARD_CUSTOM) {
printf("Board-custom event\n");
} else if (u.r.type == PD_EVENT_ACC_RW_FAIL) {
printf("RW signature check failed\n");
} else if (u.r.type == PD_EVENT_PS_FAULT) {
static const char * const fault_names[] = {
"---", "OCP", "fast OCP", "OVP", "Discharge"
};
const char *fault = u.r.data < ARRAY_SIZE(fault_names) ?
fault_names[u.r.data] : "???";
printf("Power supply fault: %s\n", fault);
} else if (u.r.type == PD_EVENT_VIDEO_DP_MODE) {
printf("DP mode %sabled\n", (u.r.data == 1) ?
"en" : "dis");
} else if (u.r.type == PD_EVENT_VIDEO_CODEC) {
memcpy(&minfo, u.r.payload,
sizeof(struct mcdp_info));
printf("HDMI info: family:%04x chipid:%04x "
"irom:%d.%d.%d fw:%d.%d.%d\n",
MCDP_FAMILY(minfo.family),
MCDP_CHIPID(minfo.chipid),
minfo.irom.major, minfo.irom.minor,
minfo.irom.build, minfo.fw.major,
minfo.fw.minor, minfo.fw.build);
} else { /* Unknown type */
int i;
printf("Event %02x (%04x) [", u.r.type, u.r.data);
for (i = 0; i < PD_LOG_SIZE(u.r.size_port); i++)
printf("%02x ", u.r.payload[i]);
printf("]\n");
}
}
return 0;
}
int cmd_pd_control(int argc, char *argv[])
{
struct ec_params_pd_control p;
int rv;
if (argc < 2) {
fprintf(stderr, "Missing parameter\n");
return -1;
}
/* Parse command */
if (!strcmp(argv[1], "reset"))
p.subcmd = PD_RESET;
else if (!strcmp(argv[1], "suspend"))
p.subcmd = PD_SUSPEND;
else if (!strcmp(argv[1], "resume"))
p.subcmd = PD_RESUME;
else if (!strcmp(argv[1], "disable"))
p.subcmd = PD_CONTROL_DISABLE;
else if (!strcmp(argv[1], "on") || !strcmp(argv[1], "chip_on"))
p.subcmd = PD_CHIP_ON;
else {
fprintf(stderr, "Unknown command: %s\n", argv[1]);
return -1;
}
if (argc == 2) {
p.chip = 0;
} else {
char *e;
p.chip = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad port number '%s'.\n", argv[2]);
return -1;
}
}
rv = ec_command(EC_CMD_PD_CONTROL, 0, &p, sizeof(p), NULL, 0);
return (rv < 0 ? rv : 0);
}
int cmd_pd_chip_info(int argc, char *argv[])
{
struct ec_params_pd_chip_info p;
struct ec_response_pd_chip_info_v1 r;
char *e;
int rv;
int cmdver = 1;
if (argc < 2 || 3 < argc) {
fprintf(stderr, "Usage: %s <port> [renew(on/off)]\n", argv[0]);
return -1;
}
p.port = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad port number.\n");
return -1;
}
p.renew = 0;
if (argc == 3) {
int val;
if (!parse_bool(argv[2], &val)) {
fprintf(stderr, "invalid arg \"%s\"\n", argv[2]);
return -1;
}
p.renew = val;
}
if (!ec_cmd_version_supported(EC_CMD_PD_CHIP_INFO, cmdver))
cmdver = 0;
rv = ec_command(EC_CMD_PD_CHIP_INFO, cmdver, &p, sizeof(p), &r,
sizeof(r));
if (rv < 0)
return rv;
printf("vendor_id: 0x%x\n", r.vendor_id);
printf("product_id: 0x%x\n", r.product_id);
printf("device_id: 0x%x\n", r.device_id);
if (r.fw_version_number != -1)
printf("fw_version: 0x%" PRIx64 "\n", r.fw_version_number);
else
printf("fw_version: UNSUPPORTED\n");
if (cmdver >= 1)
printf("min_req_fw_version: 0x%" PRIx64 "\n",
r.min_req_fw_version_number);
else
printf("min_req_fw_version: UNSUPPORTED\n");
return 0;
}
int cmd_pd_write_log(int argc, char *argv[])
{
struct ec_params_pd_write_log_entry p;
char *e;
if (argc < 3) {
fprintf(stderr, "Usage: %s <log_type> <port>\n",
argv[0]);
return -1;
}
if (!strcasecmp(argv[1], "charge"))
p.type = PD_EVENT_MCU_CHARGE;
else {
p.type = strtol(argv[1], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad log_type parameter.\n");
return -1;
}
}
p.port = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad port parameter.\n");
return -1;
}
return ec_command(EC_CMD_PD_WRITE_LOG_ENTRY, 0, &p, sizeof(p), NULL, 0);
}
int cmd_tp_self_test(int argc, char* argv[])
{
int rv;
rv = ec_command(EC_CMD_TP_SELF_TEST, 0, NULL, 0, NULL, 0);
if (rv < 0)
return rv;
printf("Touchpad self test: %s\n",
rv == EC_RES_SUCCESS ? "passed" : "failed");
return rv;
}
int cmd_tp_frame_get(int argc, char* argv[])
{
int i, j;
uint32_t remaining = 0, offset = 0;
int rv = EC_SUCCESS;
uint8_t *data;
struct ec_response_tp_frame_info* r;
struct ec_params_tp_frame_get p;
data = malloc(ec_max_insize);
r = malloc(ec_max_insize);
rv = ec_command(EC_CMD_TP_FRAME_INFO, 0, NULL, 0, r, ec_max_insize);
if (rv < 0) {
fprintf(stderr, "Failed to get toucpad frame info.\n");
goto err;
}
rv = ec_command(EC_CMD_TP_FRAME_SNAPSHOT, 0, NULL, 0, NULL, 0);
if (rv < 0) {
fprintf(stderr, "Failed to snapshot frame.\n");
goto err;
}
for (i = 0; i < r->n_frames; i++) {
p.frame_index = i;
offset = 0;
remaining = r->frame_sizes[i];
while (remaining > 0) {
p.offset = offset;
p.size = MIN(remaining, ec_max_insize);
rv = ec_command(EC_CMD_TP_FRAME_GET, 0,
&p, sizeof(p), data, p.size);
if (rv < 0) {
fprintf(stderr, "Failed to get frame data "
"at offset 0x%x\n", offset);
goto err;
}
for (j = 0; j < p.size; j++)
printf("%02x ", data[j]);
offset += p.size;
remaining -= p.size;
}
printf("\n");
}
err:
free(data);
free(r);
return rv < 0;
}
static int wait_event(long event_type,
struct ec_response_get_next_event_v1 *buffer,
size_t buffer_size, long timeout)
{
int rv;
rv = ec_pollevent(1 << event_type, buffer, buffer_size, timeout);
if (rv == 0) {
fprintf(stderr, "Timeout waiting for MKBP event\n");
return -ETIMEDOUT;
} else if (rv < 0) {
perror("Error polling for MKBP event\n");
return -EIO;
}
return rv;
}
int cmd_wait_event(int argc, char *argv[])
{
int rv, i;
struct ec_response_get_next_event_v1 buffer;
long timeout = 5000;
long event_type;
char *e;
if (!ec_pollevent) {
fprintf(stderr, "Polling for MKBP event not supported\n");
return -EINVAL;
}
if (argc < 2) {
fprintf(stderr, "Usage: %s <type> [<timeout>]\n",
argv[0]);
return -1;
}
event_type = strtol(argv[1], &e, 0);
if ((e && *e) || event_type < 0 || event_type >= EC_MKBP_EVENT_COUNT) {
fprintf(stderr, "Bad event type '%s'.\n", argv[1]);
return -1;
}
if (argc >= 3) {
timeout = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad timeout value '%s'.\n", argv[2]);
return -1;
}
}
rv = wait_event(event_type, &buffer, sizeof(buffer), timeout);
if (rv < 0)
return rv;
printf("MKBP event %d data: ", buffer.event_type);
for (i = 0; i < rv - 1; ++i)
printf("%02x ", buffer.data.key_matrix[i]);
printf("\n");
return 0;
}
static void cmd_cec_help(const char *cmd)
{
fprintf(stderr,
" Usage: %s write [write bytes...]\n"
" Write message on the CEC bus\n"
" Usage: %s read [timeout]\n"
" [timeout] in seconds\n"
" Usage: %s get <param>\n"
" Usage: %s set <param> <val>\n"
" <param> is one of:\n"
" address: CEC receive address\n"
" <val> is the new CEC address\n"
" enable: Enable or disable CEC\n"
" <val> is 1 to enable, 0 to disable\n",
cmd, cmd, cmd, cmd);
}
static int cmd_cec_write(int argc, char *argv[])
{
char *e;
long val;
int rv, i, msg_len;
struct ec_params_cec_write p;
struct ec_response_get_next_event_v1 buffer;
if (argc < 3 || argc > 18) {
fprintf(stderr, "Invalid number of params\n");
cmd_cec_help(argv[0]);
return -1;
}
msg_len = argc - 2;
for (i = 0; i < msg_len; i++) {
val = strtol(argv[i + 2], &e, 16);
if (e && *e)
return -1;
if (val < 0 || val > 0xff)
return -1;
p.msg[i] = (uint8_t)val;
}
printf("Write to CEC: ");
for (i = 0; i < msg_len; i++)
printf("0x%02x ", p.msg[i]);
printf("\n");
rv = ec_command(EC_CMD_CEC_WRITE_MSG, 0, &p, msg_len, NULL, 0);
if (rv < 0)
return rv;
rv = wait_event(EC_MKBP_EVENT_CEC_EVENT, &buffer, sizeof(buffer), 1000);
if (rv < 0)
return rv;
if (buffer.data.cec_events & EC_MKBP_CEC_SEND_OK)
return 0;
if (buffer.data.cec_events & EC_MKBP_CEC_SEND_FAILED) {
fprintf(stderr, "Send failed\n");
return -1;
}
fprintf(stderr, "No send result received\n");
return -1;
}
static int cmd_cec_read(int argc, char *argv[])
{
int i, rv;
char *e;
struct ec_response_get_next_event_v1 buffer;
long timeout = 5000;
if (!ec_pollevent) {
fprintf(stderr, "Polling for MKBP event not supported\n");
return -EINVAL;
}
if (argc >= 3) {
timeout = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad timeout value '%s'.\n", argv[2]);
return -1;
}
}
rv = wait_event(EC_MKBP_EVENT_CEC_MESSAGE, &buffer,
sizeof(buffer), timeout);
if (rv < 0)
return rv;
printf("CEC data: ");
for (i = 0; i < rv - 1; i++)
printf("0x%02x ", buffer.data.cec_message[i]);
printf("\n");
return 0;
}
static int cec_cmd_from_str(const char *str)
{
if (!strcmp("address", str))
return CEC_CMD_LOGICAL_ADDRESS;
if (!strcmp("enable", str))
return CEC_CMD_ENABLE;
return -1;
}
static int cmd_cec_set(int argc, char *argv[])
{
char *e;
struct ec_params_cec_set p;
uint8_t val;
int cmd;
if (argc != 4) {
fprintf(stderr, "Invalid number of params\n");
cmd_cec_help(argv[0]);
return -1;
}
val = (uint8_t)strtol(argv[3], &e, 0);
if (e && *e) {
fprintf(stderr, "Bad parameter '%s'.\n", argv[3]);
return -1;
}
cmd = cec_cmd_from_str(argv[2]);
if (cmd < 0) {
fprintf(stderr, "Invalid command '%s'.\n", argv[2]);
return -1;
}
p.cmd = cmd;
p.val = val;
return ec_command(EC_CMD_CEC_SET,
0, &p, sizeof(p), NULL, 0);
}
static int cmd_cec_get(int argc, char *argv[])
{
int rv, cmd;
struct ec_params_cec_get p;
struct ec_response_cec_get r;
if (argc != 3) {
fprintf(stderr, "Invalid number of params\n");
cmd_cec_help(argv[0]);
return -1;
}
cmd = cec_cmd_from_str(argv[2]);
if (cmd < 0) {
fprintf(stderr, "Invalid command '%s'.\n", argv[2]);
return -1;
}
p.cmd = cmd;
rv = ec_command(EC_CMD_CEC_GET, 0, &p, sizeof(p), &r, sizeof(r));
if (rv < 0)
return rv;
printf("%d\n", r.val);
return 0;
}
int cmd_cec(int argc, char *argv[])
{
if (argc < 2) {
fprintf(stderr, "Invalid number of params\n");
cmd_cec_help(argv[0]);
return -1;
}
if (!strcmp(argv[1], "write"))
return cmd_cec_write(argc, argv);
if (!strcmp(argv[1], "read"))
return cmd_cec_read(argc, argv);
if (!strcmp(argv[1], "get"))
return cmd_cec_get(argc, argv);
if (!strcmp(argv[1], "set"))
return cmd_cec_set(argc, argv);
fprintf(stderr, "Invalid sub command: %s\n", argv[1]);
cmd_cec_help(argv[0]);
return -1;
}
/* NULL-terminated list of commands */
const struct command commands[] = {
{"adcread", cmd_adc_read},
{"addentropy", cmd_add_entropy},
{"apreset", cmd_apreset},
{"autofanctrl", cmd_thermal_auto_fan_ctrl},
{"backlight", cmd_lcd_backlight},
{"battery", cmd_battery},
{"batterycutoff", cmd_battery_cut_off},
{"batteryparam", cmd_battery_vendor_param},
{"boardversion", cmd_board_version},
{"cbi", cmd_cbi},
{"chargecurrentlimit", cmd_charge_current_limit},
{"chargecontrol", cmd_charge_control},
{"chargeoverride", cmd_charge_port_override},
{"chargestate", cmd_charge_state},
{"chipinfo", cmd_chipinfo},
{"cmdversions", cmd_cmdversions},
{"console", cmd_console},
{"cec", cmd_cec},
{"echash", cmd_ec_hash},
{"eventclear", cmd_host_event_clear},
{"eventclearb", cmd_host_event_clear_b},
{"eventget", cmd_host_event_get_raw},
{"eventgetb", cmd_host_event_get_b},
{"eventgetscimask", cmd_host_event_get_sci_mask},
{"eventgetsmimask", cmd_host_event_get_smi_mask},
{"eventgetwakemask", cmd_host_event_get_wake_mask},
{"eventsetscimask", cmd_host_event_set_sci_mask},
{"eventsetsmimask", cmd_host_event_set_smi_mask},
{"eventsetwakemask", cmd_host_event_set_wake_mask},
{"extpwrlimit", cmd_ext_power_limit},
{"fanduty", cmd_fanduty},
{"flasherase", cmd_flash_erase},
{"flashprotect", cmd_flash_protect},
{"flashread", cmd_flash_read},
{"flashwrite", cmd_flash_write},
{"flashinfo", cmd_flash_info},
{"flashspiinfo", cmd_flash_spi_info},
{"flashpd", cmd_flash_pd},
{"forcelidopen", cmd_force_lid_open},
{"fpframe", cmd_fp_frame},
{"fpinfo", cmd_fp_info},
{"fpmode", cmd_fp_mode},
{"fpseed", cmd_fp_seed},
{"fpstats", cmd_fp_stats},
{"fptemplate", cmd_fp_template},
{"gpioget", cmd_gpio_get},
{"gpioset", cmd_gpio_set},
{"hangdetect", cmd_hang_detect},
{"hello", cmd_hello},
{"hibdelay", cmd_hibdelay},
{"hostsleepstate", cmd_hostsleepstate},
{"i2cprotect", cmd_i2c_protect},
{"i2cread", cmd_i2c_read},
{"i2cwrite", cmd_i2c_write},
{"i2cxfer", cmd_i2c_xfer},
{"infopddev", cmd_pd_device_info},
{"inventory", cmd_inventory},
{"led", cmd_led},
{"lightbar", cmd_lightbar},
{"kbfactorytest", cmd_keyboard_factory_test},
{"kbid", cmd_kbid},
{"kbinfo", cmd_kbinfo},
{"kbpress", cmd_kbpress},
{"keyconfig", cmd_keyconfig},
{"keyscan", cmd_keyscan},
{"motionsense", cmd_motionsense},
{"nextevent", cmd_next_event},
{"panicinfo", cmd_panic_info},
{"pause_in_s5", cmd_s5},
{"pdgetmode", cmd_pd_get_amode},
{"pdsetmode", cmd_pd_set_amode},
{"port80read", cmd_port80_read},
{"pdlog", cmd_pd_log},
{"pdcontrol", cmd_pd_control},
{"pdchipinfo", cmd_pd_chip_info},
{"pdwritelog", cmd_pd_write_log},
{"powerinfo", cmd_power_info},
{"protoinfo", cmd_proto_info},
{"pstoreinfo", cmd_pstore_info},
{"pstoreread", cmd_pstore_read},
{"pstorewrite", cmd_pstore_write},
{"pwmgetfanrpm", cmd_pwm_get_fan_rpm},
{"pwmgetkblight", cmd_pwm_get_keyboard_backlight},
{"pwmgetnumfans", cmd_pwm_get_num_fans},
{"pwmgetduty", cmd_pwm_get_duty},
{"pwmsetfanrpm", cmd_pwm_set_fan_rpm},
{"pwmsetkblight", cmd_pwm_set_keyboard_backlight},
{"pwmsetduty", cmd_pwm_set_duty},
{"readtest", cmd_read_test},
{"reboot_ec", cmd_reboot_ec},
{"rollbackinfo", cmd_rollback_info},
{"rtcget", cmd_rtc_get},
{"rtcgetalarm", cmd_rtc_get_alarm},
{"rtcset", cmd_rtc_set},
{"rtcsetalarm", cmd_rtc_set_alarm},
{"rwhashpd", cmd_rw_hash_pd},
{"rwsigaction", cmd_rwsig_action},
{"rwsigstatus", cmd_rwsig_status},
{"sertest", cmd_serial_test},
{"port80flood", cmd_port_80_flood},
{"switches", cmd_switches},
{"temps", cmd_temperature},
{"tempsinfo", cmd_temp_sensor_info},
{"test", cmd_test},
{"thermalget", cmd_thermal_get_threshold},
{"thermalset", cmd_thermal_set_threshold},
{"tpselftest", cmd_tp_self_test},
{"tpframeget", cmd_tp_frame_get},
{"tmp006cal", cmd_tmp006cal},
{"tmp006raw", cmd_tmp006raw},
{"uptimeinfo", cmd_uptimeinfo},
{"usbchargemode", cmd_usb_charge_set_mode},
{"usbmux", cmd_usb_mux},
{"usbpd", cmd_usb_pd},
{"usbpdmuxinfo", cmd_usb_pd_mux_info},
{"usbpdpower", cmd_usb_pd_power},
{"version", cmd_version},
{"waitevent", cmd_wait_event},
{"wireless", cmd_wireless},
{NULL, NULL}
};
int main(int argc, char *argv[])
{
const struct command *cmd;
int dev = 0;
int interfaces = COMM_ALL;
char device_name[41] = CROS_EC_DEV_NAME;
int rv = 1;
int parse_error = 0;
char *e;
int i;
BUILD_ASSERT(ARRAY_SIZE(lb_command_paramcount) == LIGHTBAR_NUM_CMDS);
while ((i = getopt_long(argc, argv, "?", long_opts, NULL)) != -1) {
switch (i) {
case '?':
/* Unhandled option */
parse_error = 1;
break;
case OPT_DEV:
dev = strtoul(optarg, &e, 0);
if (!*optarg || (e && *e)) {
fprintf(stderr, "Invalid --dev\n");
parse_error = 1;
}
break;
case OPT_INTERFACE:
if (!strcasecmp(optarg, "dev")) {
interfaces = COMM_DEV;
} else if (!strcasecmp(optarg, "lpc")) {
interfaces = COMM_LPC;
} else if (!strcasecmp(optarg, "i2c")) {
interfaces = COMM_I2C;
} else if (!strcasecmp(optarg, "servo")) {
interfaces = COMM_SERVO;
} else {
fprintf(stderr, "Invalid --interface\n");
parse_error = 1;
}
break;
case OPT_NAME:
strncpy(device_name, optarg, 40);
device_name[40] = '\0';
break;
case OPT_ASCII:
ascii_mode = 1;
break;
}
}
/* Must specify a command */
if (!parse_error && optind == argc)
parse_error = 1;
/* 'ectool help' prints help with commands */
if (!parse_error && !strcasecmp(argv[optind], "help")) {
print_help(argv[0], 1);
exit(1);
}
/* Handle sub-devices command offset */
if (dev > 0 && dev < 4) {
set_command_offset(EC_CMD_PASSTHRU_OFFSET(dev));
} else if (dev == 8) {
/* Special offset for Fingerprint MCU */
strcpy(device_name, "cros_fp");
} else if (dev != 0) {
fprintf(stderr, "Bad device number %d\n", dev);
parse_error = 1;
}
if (parse_error) {
print_help(argv[0], 0);
exit(1);
}
/* Prefer /dev method, which supports built-in mutex */
if (!(interfaces & COMM_DEV) || comm_init_dev(device_name)) {
/* If dev is excluded or isn't supported, find alternative */
if (acquire_gec_lock(GEC_LOCK_TIMEOUT_SECS) < 0) {
fprintf(stderr, "Could not acquire GEC lock.\n");
exit(1);
}
if (comm_init_alt(interfaces, device_name)) {
fprintf(stderr, "Couldn't find EC\n");
goto out;
}
}
if (comm_init_buffer()) {
fprintf(stderr, "Couldn't initialize buffers\n");
goto out;
}
/* Handle commands */
for (cmd = commands; cmd->name; cmd++) {
if (!strcasecmp(argv[optind], cmd->name)) {
rv = cmd->handler(argc - optind, argv + optind);
goto out;
}
}
/* If we're still here, command was unknown */
fprintf(stderr, "Unknown command '%s'\n\n", argv[optind]);
print_help(argv[0], 0);
out:
release_gec_lock();
return !!rv;
}
|