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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2012 Gary Mills
* Copyright 2020 Joyent, Inc.
* Copyright 2025 Oxide Computer Company
*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Boot console support. Most of the file is shared between dboot, and the
* early kernel / fakebop.
*/
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/archsystm.h>
#include <sys/framebuffer.h>
#include <sys/boot_console.h>
#include <sys/panic.h>
#include <sys/ctype.h>
#include <sys/ascii.h>
#include <sys/vgareg.h>
#if defined(__xpv)
#include <sys/hypervisor.h>
#endif /* __xpv */
#include "boot_console_impl.h"
#include "boot_serial.h"
#if defined(_BOOT)
#include <dboot/dboot_asm.h>
#include <dboot/dboot_xboot.h>
#else /* _BOOT */
#include <sys/bootconf.h>
#if defined(__xpv)
#include <sys/evtchn_impl.h>
#endif /* __xpv */
static char *defcons_buf;
static char *defcons_cur;
#endif /* _BOOT */
#if defined(__xpv)
extern void bcons_init_xen(char *);
extern void bcons_putchar_xen(int);
extern int bcons_getchar_xen(void);
extern int bcons_ischar_xen(void);
#endif /* __xpv */
fb_info_t fb_info;
static bcons_dev_t bcons_dev; /* Device callbacks */
static int console = CONS_SCREEN_TEXT;
static int diag = CONS_INVALID;
static int tty_num = 0;
static int tty_addr[] = {0x3f8, 0x2f8, 0x3e8, 0x2e8};
static char *boot_line;
static struct boot_env {
char *be_env; /* ends with double ascii nul */
size_t be_size; /* size of the environment, including nul */
} boot_env;
/*
* Simple console terminal emulator for early boot.
* We need this to support kmdb, all other console output is supposed
* to be simple text output.
*/
typedef enum btem_state_type {
A_STATE_START,
A_STATE_ESC,
A_STATE_CSI,
A_STATE_CSI_QMARK,
A_STATE_CSI_EQUAL
} btem_state_type_t;
#define BTEM_MAXPARAMS 5
typedef struct btem_state {
btem_state_type_t btem_state;
boolean_t btem_gotparam;
int btem_curparam;
int btem_paramval;
int btem_params[BTEM_MAXPARAMS];
} btem_state_t;
static btem_state_t boot_tem;
static int serial_ischar(void);
static int serial_getchar(void);
static void serial_putchar(int);
static void serial_adjust_prop(void);
static void defcons_putchar(int);
#if !defined(_BOOT)
static boolean_t bootprop_set_tty_mode;
#endif
#if defined(__xpv)
static int console_hypervisor_redirect = B_FALSE;
static int console_hypervisor_device = CONS_INVALID;
static int console_hypervisor_tty_num = 0;
/* Obtain the hypervisor console type */
int
console_hypervisor_dev_type(int *tnum)
{
if (tnum != NULL)
*tnum = console_hypervisor_tty_num;
return (console_hypervisor_device);
}
#endif /* __xpv */
static int port;
static void
serial_init(void)
{
port = tty_addr[tty_num];
outb(port + ISR, 0x20);
if (inb(port + ISR) & 0x20) {
/*
* 82510 chip is present
*/
outb(port + DAT+7, 0x04); /* clear status */
outb(port + ISR, 0x40); /* set to bank 2 */
outb(port + MCR, 0x08); /* IMD */
outb(port + DAT, 0x21); /* FMD */
outb(port + ISR, 0x00); /* set to bank 0 */
} else {
/*
* set the UART in FIFO mode if it has FIFO buffers.
* use 16550 fifo reset sequence specified in NS
* application note. disable fifos until chip is
* initialized.
*/
outb(port + FIFOR, 0x00); /* clear */
outb(port + FIFOR, FIFO_ON); /* enable */
outb(port + FIFOR, FIFO_ON|FIFORXFLSH); /* reset */
outb(port + FIFOR,
FIFO_ON|FIFODMA|FIFOTXFLSH|FIFORXFLSH|0x80);
if ((inb(port + ISR) & 0xc0) != 0xc0) {
/*
* no fifo buffers so disable fifos.
* this is true for 8250's
*/
outb(port + FIFOR, 0x00);
}
}
/* disable interrupts */
outb(port + ICR, 0);
#if !defined(_BOOT)
if (IN_XPV_PANIC())
return;
#endif
/* adjust setting based on tty properties */
serial_adjust_prop();
}
/* Advance str pointer past white space */
#define EAT_WHITE_SPACE(str) { \
while ((*str != '\0') && ISSPACE(*str)) \
str++; \
}
/*
* boot_line is set when we call here. Search it for the argument name,
* and if found, return a pointer to it.
*/
static char *
find_boot_line_prop(const char *name)
{
char *ptr;
char *ret = NULL;
char end_char;
size_t len;
if (boot_line == NULL)
return (NULL);
len = strlen(name);
/*
* We have two nested loops here: the outer loop discards all options
* except -B, and the inner loop parses the -B options looking for
* the one we're interested in.
*/
for (ptr = boot_line; *ptr != '\0'; ptr++) {
EAT_WHITE_SPACE(ptr);
if (*ptr == '-') {
ptr++;
while ((*ptr != '\0') && (*ptr != 'B') &&
!ISSPACE(*ptr))
ptr++;
if (*ptr == '\0')
goto out;
else if (*ptr != 'B')
continue;
} else {
while ((*ptr != '\0') && !ISSPACE(*ptr))
ptr++;
if (*ptr == '\0')
goto out;
continue;
}
do {
ptr++;
EAT_WHITE_SPACE(ptr);
if ((strncmp(ptr, name, len) == 0) &&
(ptr[len] == '=')) {
ptr += len + 1;
if ((*ptr == '\'') || (*ptr == '"')) {
ret = ptr + 1;
end_char = *ptr;
ptr++;
} else {
ret = ptr;
end_char = ',';
}
goto consume_property;
}
/*
* We have a property, and it's not the one we're
* interested in. Skip the property name. A name
* can end with '=', a comma, or white space.
*/
while ((*ptr != '\0') && (*ptr != '=') &&
(*ptr != ',') && (!ISSPACE(*ptr)))
ptr++;
/*
* We only want to go through the rest of the inner
* loop if we have a comma. If we have a property
* name without a value, either continue or break.
*/
if (*ptr == '\0')
goto out;
else if (*ptr == ',')
continue;
else if (ISSPACE(*ptr))
break;
ptr++;
/*
* Is the property quoted?
*/
if ((*ptr == '\'') || (*ptr == '"')) {
end_char = *ptr;
ptr++;
} else {
/*
* Not quoted, so the string ends at a comma
* or at white space. Deal with white space
* later.
*/
end_char = ',';
}
/*
* Now, we can ignore any characters until we find
* end_char.
*/
consume_property:
for (; (*ptr != '\0') && (*ptr != end_char); ptr++) {
if ((end_char == ',') && ISSPACE(*ptr))
break;
}
if (*ptr && (*ptr != ',') && !ISSPACE(*ptr))
ptr++;
} while (*ptr == ',');
}
out:
return (ret);
}
/*
* Find prop from boot env module. The data in module is list of C strings
* name=value, the list is terminated by double nul.
*/
static const char *
find_boot_env_prop(const char *name)
{
char *ptr;
size_t len;
uintptr_t size;
if (boot_env.be_env == NULL)
return (NULL);
ptr = boot_env.be_env;
len = strlen(name);
/*
* Make sure we have at least len + 2 bytes in the environment.
* We are looking for name=value\0 constructs, and the environment
* itself is terminated by '\0'.
*/
if (boot_env.be_size < len + 2)
return (NULL);
do {
if ((strncmp(ptr, name, len) == 0) && (ptr[len] == '=')) {
ptr += len + 1;
return (ptr);
}
/* find the first '\0' */
while (*ptr != '\0') {
ptr++;
size = (uintptr_t)ptr - (uintptr_t)boot_env.be_env;
if (size > boot_env.be_size)
return (NULL);
}
ptr++;
/* If the remainder is shorter than name + 2, get out. */
size = (uintptr_t)ptr - (uintptr_t)boot_env.be_env;
if (boot_env.be_size - size < len + 2)
return (NULL);
} while (*ptr != '\0');
return (NULL);
}
/*
* Get prop value from either command line or boot environment.
* We always check kernel command line first, as this will keep the
* functionality and will allow user to override the values in environment.
*/
const char *
find_boot_prop(const char *name)
{
const char *value = find_boot_line_prop(name);
if (value == NULL)
value = find_boot_env_prop(name);
return (value);
}
#define MATCHES(p, pat) \
(strncmp(p, pat, strlen(pat)) == 0 ? (p += strlen(pat), 1) : 0)
#define SKIP(p, c) \
while (*(p) != 0 && *p != (c)) \
++(p); \
if (*(p) == (c)) \
++(p);
/*
* find a tty mode property either from cmdline or from boot properties
*/
static const char *
get_mode_value(char *name)
{
/*
* when specified on boot line it looks like "name" "="....
*/
if (boot_line != NULL) {
return (find_boot_prop(name));
}
#if defined(_BOOT)
return (NULL);
#else
/*
* if we're running in the full kernel we check the bootenv.rc settings
*/
{
static char propval[20];
propval[0] = 0;
if (do_bsys_getproplen(NULL, name) <= 0)
return (NULL);
(void) do_bsys_getprop(NULL, name, propval);
return (propval);
}
#endif
}
/*
* adjust serial port based on properties
* These come either from the cmdline or from boot properties.
*/
static void
serial_adjust_prop(void)
{
char propname[20];
const char *propval;
const char *p;
ulong_t baud;
uchar_t lcr = 0;
uchar_t mcr = DTR | RTS;
(void) strcpy(propname, "ttyX-mode");
propname[3] = 'a' + tty_num;
propval = get_mode_value(propname);
#if !defined(_BOOT)
if (propval != NULL)
bootprop_set_tty_mode = B_TRUE;
#endif
if (propval == NULL)
propval = "9600,8,n,1,-";
/* property is of the form: "9600,8,n,1,-" */
p = propval;
if (MATCHES(p, "110,"))
baud = ASY110;
else if (MATCHES(p, "150,"))
baud = ASY150;
else if (MATCHES(p, "300,"))
baud = ASY300;
else if (MATCHES(p, "600,"))
baud = ASY600;
else if (MATCHES(p, "1200,"))
baud = ASY1200;
else if (MATCHES(p, "2400,"))
baud = ASY2400;
else if (MATCHES(p, "4800,"))
baud = ASY4800;
else if (MATCHES(p, "19200,"))
baud = ASY19200;
else if (MATCHES(p, "38400,"))
baud = ASY38400;
else if (MATCHES(p, "57600,"))
baud = ASY57600;
else if (MATCHES(p, "115200,"))
baud = ASY115200;
else {
baud = ASY9600;
SKIP(p, ',');
}
outb(port + LCR, DLAB);
outb(port + DAT + DLL, baud & 0xff);
outb(port + DAT + DLH, (baud >> 8) & 0xff);
switch (*p) {
case '5':
lcr |= BITS5;
++p;
break;
case '6':
lcr |= BITS6;
++p;
break;
case '7':
lcr |= BITS7;
++p;
break;
case '8':
++p;
/* FALLTHROUGH */
default:
lcr |= BITS8;
break;
}
SKIP(p, ',');
switch (*p) {
case 'n':
lcr |= PARITY_NONE;
++p;
break;
case 'o':
lcr |= PARITY_ODD;
++p;
break;
case 'e':
++p;
/* FALLTHROUGH */
default:
lcr |= PARITY_EVEN;
break;
}
SKIP(p, ',');
switch (*p) {
case '1':
/* STOP1 is 0 */
++p;
break;
default:
lcr |= STOP2;
break;
}
/* set parity bits */
outb(port + LCR, lcr);
(void) strcpy(propname, "ttyX-rts-dtr-off");
propname[3] = 'a' + tty_num;
propval = get_mode_value(propname);
if (propval == NULL)
propval = "false";
if (propval[0] != 'f' && propval[0] != 'F')
mcr = 0;
/* set modem control bits */
outb(port + MCR, mcr | OUT2);
}
/* Obtain the console type */
int
boot_console_type(int *tnum)
{
if (tnum != NULL)
*tnum = tty_num;
return (console);
}
/*
* A structure to map console names to values.
*/
typedef struct {
char *name;
int value;
} console_value_t;
console_value_t console_devices[] = {
{ "ttya", CONS_TTY }, /* 0 */
{ "ttyb", CONS_TTY }, /* 1 */
{ "ttyc", CONS_TTY }, /* 2 */
{ "ttyd", CONS_TTY }, /* 3 */
{ "text", CONS_SCREEN_TEXT },
{ "graphics", CONS_SCREEN_GRAPHICS },
#if defined(__xpv)
{ "hypervisor", CONS_HYPERVISOR },
#endif
#if !defined(_BOOT)
{ "usb-serial", CONS_USBSER },
#endif
{ NULL, CONS_INVALID }
};
static void
bcons_init_env(struct xboot_info *xbi)
{
uint32_t i;
struct boot_modules *modules;
modules = (struct boot_modules *)(uintptr_t)xbi->bi_modules;
for (i = 0; i < xbi->bi_module_cnt; i++) {
if (modules[i].bm_type == BMT_ENV)
break;
}
if (i == xbi->bi_module_cnt)
return;
boot_env.be_env = (char *)(uintptr_t)modules[i].bm_addr;
boot_env.be_size = modules[i].bm_size;
}
int
boot_fb(struct xboot_info *xbi, int console)
{
if (xbi_fb_init(xbi, &bcons_dev) == B_FALSE)
return (console);
/*
* The framebuffer address is not set; fall back to the serial console.
*/
if (fb_info.paddr == 0)
return (CONS_TTY);
#if defined(_BOOT)
/*
* If the firmware placed the framebuffer mapping above the 32-bit
* boundary, we cannot use it from dboot.
*/
if (fb_info.paddr >= UINTPTR_MAX)
return (CONS_TTY);
#endif
fb_info.terminal.x = VGA_TEXT_COLS;
fb_info.terminal.y = VGA_TEXT_ROWS;
boot_fb_init(CONS_FRAMEBUFFER);
if (console == CONS_SCREEN_TEXT)
return (CONS_FRAMEBUFFER);
return (console);
}
/*
* TODO.
* quick and dirty local atoi. Perhaps should build with strtol, but
* dboot & early boot mix does overcomplicate things much.
* Stolen from libc anyhow.
*/
static int
atoi(const char *p)
{
int n, c, neg = 0;
unsigned char *up = (unsigned char *)p;
if (!isdigit(c = *up)) {
while (isspace(c))
c = *++up;
switch (c) {
case '-':
neg++;
/* FALLTHROUGH */
case '+':
c = *++up;
}
if (!isdigit(c))
return (0);
}
for (n = '0' - c; isdigit(c = *++up);) {
n *= 10; /* two steps to avoid unnecessary overflow */
n += '0' - c; /* accum neg to avoid surprises at MAX */
}
return (neg ? n : -n);
}
static void
bcons_init_fb(void)
{
const char *propval;
int intval;
/* initialize with explicit default values */
fb_info.fg_color = CONS_COLOR;
fb_info.bg_color = 0;
fb_info.inverse = B_FALSE;
fb_info.inverse_screen = B_FALSE;
/* color values are 0 - 255 */
propval = find_boot_prop("tem.fg_color");
if (propval != NULL) {
intval = atoi(propval);
if (intval >= 0 && intval <= 255)
fb_info.fg_color = intval;
}
/* color values are 0 - 255 */
propval = find_boot_prop("tem.bg_color");
if (propval != NULL && ISDIGIT(*propval)) {
intval = atoi(propval);
if (intval >= 0 && intval <= 255)
fb_info.bg_color = intval;
}
/* get inverses. allow 0, 1, true, false */
propval = find_boot_prop("tem.inverse");
if (propval != NULL) {
if (*propval == '1' || MATCHES(propval, "true"))
fb_info.inverse = B_TRUE;
}
propval = find_boot_prop("tem.inverse-screen");
if (propval != NULL) {
if (*propval == '1' || MATCHES(propval, "true"))
fb_info.inverse_screen = B_TRUE;
}
#if defined(_BOOT)
/*
* Load cursor position from bootloader only in dboot,
* dboot will pass cursor position to kernel via xboot info.
*/
propval = find_boot_prop("tem.cursor.row");
if (propval != NULL) {
intval = atoi(propval);
if (intval >= 0 && intval <= 0xFFFF)
fb_info.cursor.pos.y = intval;
}
propval = find_boot_prop("tem.cursor.col");
if (propval != NULL) {
intval = atoi(propval);
if (intval >= 0 && intval <= 0xFFFF)
fb_info.cursor.pos.x = intval;
}
#endif
}
/*
* Go through the known console device names trying to match the string we were
* given. The string on the command line must end with a comma or white space.
*
* For convenience, we provide the caller with an integer index for the CONS_TTY
* case.
*/
static int
lookup_console_device(const char *cons_str, int *indexp)
{
int n, cons;
size_t len, cons_len;
console_value_t *consolep;
cons = CONS_INVALID;
if (cons_str != NULL) {
cons_len = strlen(cons_str);
for (n = 0; console_devices[n].name != NULL; n++) {
consolep = &console_devices[n];
len = strlen(consolep->name);
if ((len <= cons_len) && ((cons_str[len] == '\0') ||
(cons_str[len] == ',') || (cons_str[len] == '\'') ||
(cons_str[len] == '"') || ISSPACE(cons_str[len])) &&
(strncmp(cons_str, consolep->name, len) == 0)) {
cons = consolep->value;
if (cons == CONS_TTY)
*indexp = n;
break;
}
}
}
return (cons);
}
void
bcons_init(struct xboot_info *xbi)
{
const char *cons_str;
#if !defined(_BOOT)
static char console_text[] = "text";
extern int post_fastreboot;
#endif
if (xbi == NULL) {
/* This is very early dboot console, set up ttya. */
console = CONS_TTY;
serial_init();
return;
}
/* Set up data to fetch properties from commad line and boot env. */
boot_line = (char *)(uintptr_t)xbi->bi_cmdline;
bcons_init_env(xbi);
console = CONS_INVALID;
/* set up initial fb_info */
bcons_init_fb();
#if defined(__xpv)
bcons_init_xen(boot_line);
#endif /* __xpv */
/*
* First check for diag-device.
*/
cons_str = find_boot_prop("diag-device");
if (cons_str != NULL)
diag = lookup_console_device(cons_str, &tty_num);
cons_str = find_boot_prop("console");
if (cons_str == NULL)
cons_str = find_boot_prop("output-device");
#if !defined(_BOOT)
if (post_fastreboot && strcmp(cons_str, "graphics") == 0)
cons_str = console_text;
#endif
if (cons_str != NULL)
console = lookup_console_device(cons_str, &tty_num);
#if defined(__xpv)
/*
* domU's always use the hypervisor regardless of what
* the console variable may be set to.
*/
if (!DOMAIN_IS_INITDOMAIN(xen_info)) {
console = CONS_HYPERVISOR;
console_hypervisor_redirect = B_TRUE;
}
#endif /* __xpv */
if (console == CONS_INVALID)
console = CONS_SCREEN_TEXT;
#if defined(__xpv)
if (DOMAIN_IS_INITDOMAIN(xen_info)) {
switch (HYPERVISOR_console_io(CONSOLEIO_get_device, 0, NULL)) {
case XEN_CONSOLE_COM1:
case XEN_CONSOLE_COM2:
console_hypervisor_device = CONS_TTY;
console_hypervisor_tty_num = tty_num;
break;
case XEN_CONSOLE_VGA:
/*
* Currently xen doesn't really support
* keyboard/display console devices.
* What this setting means is that
* "vga=keep" has been enabled, which is
* more of a xen debugging tool that a
* true console mode. Hence, we're going
* to ignore this xen "console" setting.
*/
/*FALLTHROUGH*/
default:
console_hypervisor_device = CONS_INVALID;
}
}
/*
* if the hypervisor is using the currently selected serial
* port then default to using the hypervisor as the console
* device.
*/
if (console == console_hypervisor_device) {
console = CONS_HYPERVISOR;
console_hypervisor_redirect = B_TRUE;
}
#endif /* __xpv */
/* make sure the FB is set up if present */
console = boot_fb(xbi, console);
switch (console) {
case CONS_TTY:
serial_init();
break;
case CONS_HYPERVISOR:
break;
#if !defined(_BOOT)
case CONS_USBSER:
/*
* We can't do anything with the usb serial
* until we have memory management.
*/
break;
#endif
case CONS_SCREEN_GRAPHICS:
kb_init();
break;
case CONS_SCREEN_TEXT:
boot_vga_init(&bcons_dev);
/* Fall through */
default:
kb_init();
break;
}
/*
* Initialize diag device unless already done.
*/
switch (diag) {
case CONS_TTY:
if (console != CONS_TTY)
serial_init();
break;
case CONS_SCREEN_GRAPHICS:
case CONS_SCREEN_TEXT:
if (console != CONS_SCREEN_GRAPHICS &&
console != CONS_SCREEN_TEXT)
kb_init();
break;
default:
break;
}
}
static void
serial_putchar(int c)
{
int checks = 10000;
while (((inb(port + LSR) & XHRE) == 0) && checks--)
;
outb(port + DAT, (char)c);
}
static int
serial_getchar(void)
{
uchar_t lsr;
while (serial_ischar() == 0)
;
lsr = inb(port + LSR);
if (lsr & (SERIAL_BREAK | SERIAL_FRAME |
SERIAL_PARITY | SERIAL_OVERRUN)) {
if (lsr & SERIAL_OVERRUN) {
return (inb(port + DAT));
} else {
/* Toss the garbage */
(void) inb(port + DAT);
return (0);
}
}
return (inb(port + DAT));
}
static int
serial_ischar(void)
{
return (inb(port + LSR) & RCA);
}
static void
btem_control(btem_state_t *btem, int c)
{
int y, rows, cols;
rows = fb_info.cursor.pos.y;
cols = fb_info.cursor.pos.x;
btem->btem_state = A_STATE_START;
switch (c) {
case A_BS:
bcons_dev.bd_setpos(rows, cols - 1);
break;
case A_HT:
cols += 8 - (cols % 8);
if (cols >= fb_info.terminal.x)
cols = fb_info.terminal.x - 1;
bcons_dev.bd_setpos(rows, cols);
break;
case A_CR:
bcons_dev.bd_setpos(rows, 0);
break;
case A_FF:
for (y = 0; y < fb_info.terminal.y; y++) {
bcons_dev.bd_setpos(y, 0);
bcons_dev.bd_eraseline();
}
bcons_dev.bd_setpos(0, 0);
break;
case A_ESC:
btem->btem_state = A_STATE_ESC;
break;
default:
bcons_dev.bd_putchar(c);
break;
}
}
/*
* if parameters [0..count - 1] are not set, set them to the value
* of newparam.
*/
static void
btem_setparam(btem_state_t *btem, int count, int newparam)
{
int i;
for (i = 0; i < count; i++) {
if (btem->btem_params[i] == -1)
btem->btem_params[i] = newparam;
}
}
static void
btem_chkparam(btem_state_t *btem, int c)
{
int rows, cols;
rows = fb_info.cursor.pos.y;
cols = fb_info.cursor.pos.x;
switch (c) {
case '@': /* insert char */
btem_setparam(btem, 1, 1);
bcons_dev.bd_shift(btem->btem_params[0]);
break;
case 'A': /* cursor up */
btem_setparam(btem, 1, 1);
bcons_dev.bd_setpos(rows - btem->btem_params[0], cols);
break;
case 'B': /* cursor down */
btem_setparam(btem, 1, 1);
bcons_dev.bd_setpos(rows + btem->btem_params[0], cols);
break;
case 'C': /* cursor right */
btem_setparam(btem, 1, 1);
bcons_dev.bd_setpos(rows, cols + btem->btem_params[0]);
break;
case 'D': /* cursor left */
btem_setparam(btem, 1, 1);
bcons_dev.bd_setpos(rows, cols - btem->btem_params[0]);
break;
case 'K':
bcons_dev.bd_eraseline();
break;
default:
/* bcons_dev.bd_putchar(c); */
break;
}
btem->btem_state = A_STATE_START;
}
static void
btem_chkparam_qmark(btem_state_t *btem, int c)
{
/*
* This code is intentionally NOP, we do process
* \E[?25h and \E[?25l, but our cursor is always shown.
*/
switch (c) {
case 'h': /* DEC private mode set */
btem_setparam(btem, 1, 1);
switch (btem->btem_params[0]) {
case 25: /* show cursor */
break;
}
break;
case 'l':
/* DEC private mode reset */
btem_setparam(btem, 1, 1);
switch (btem->btem_params[0]) {
case 25: /* hide cursor */
break;
}
break;
}
btem->btem_state = A_STATE_START;
}
static void
btem_getparams(btem_state_t *btem, int c)
{
if (isdigit(c)) {
btem->btem_paramval = btem->btem_paramval * 10 + c - '0';
btem->btem_gotparam = B_TRUE;
return;
}
if (btem->btem_curparam < BTEM_MAXPARAMS) {
if (btem->btem_gotparam == B_TRUE) {
btem->btem_params[btem->btem_curparam] =
btem->btem_paramval;
}
btem->btem_curparam++;
}
if (c == ';') {
/* Restart parameter search */
btem->btem_gotparam = B_FALSE;
btem->btem_paramval = 0;
} else {
if (btem->btem_state == A_STATE_CSI_QMARK)
btem_chkparam_qmark(btem, c);
else
btem_chkparam(btem, c);
}
}
/* Simple boot terminal parser. */
static void
btem_parse(btem_state_t *btem, int c)
{
int i;
/* Normal state? */
if (btem->btem_state == A_STATE_START) {
if (c == A_CSI || c < ' ')
btem_control(btem, c);
else
bcons_dev.bd_putchar(c);
return;
}
/* In <ESC> sequence */
if (btem->btem_state != A_STATE_ESC) {
if (btem->btem_state != A_STATE_CSI) {
btem_getparams(btem, c);
return;
}
switch (c) {
case '?':
btem->btem_state = A_STATE_CSI_QMARK;
return;
default:
btem_getparams(btem, c);
return;
}
}
/* Previous char was <ESC> */
switch (c) {
case '[':
btem->btem_curparam = 0;
btem->btem_paramval = 0;
btem->btem_gotparam = B_FALSE;
/* clear the parameters */
for (i = 0; i < BTEM_MAXPARAMS; i++)
btem->btem_params[i] = -1;
btem->btem_state = A_STATE_CSI;
return;
case 'Q': /* <ESC>Q */
case 'C': /* <ESC>C */
btem->btem_state = A_STATE_START;
return;
default:
btem->btem_state = A_STATE_START;
break;
}
if (c < ' ')
btem_control(btem, c);
else
bcons_dev.bd_putchar(c);
}
static void
_doputchar(int device, int c)
{
switch (device) {
case CONS_TTY:
serial_putchar(c);
return;
case CONS_SCREEN_TEXT:
case CONS_FRAMEBUFFER:
bcons_dev.bd_cursor(B_FALSE);
btem_parse(&boot_tem, c);
bcons_dev.bd_cursor(B_TRUE);
return;
case CONS_SCREEN_GRAPHICS:
#if !defined(_BOOT)
case CONS_USBSER:
defcons_putchar(c);
#endif /* _BOOT */
default:
return;
}
}
void
bcons_putchar(int c)
{
#if defined(__xpv)
if (!DOMAIN_IS_INITDOMAIN(xen_info) ||
console == CONS_HYPERVISOR) {
bcons_putchar_xen(c);
return;
}
#endif /* __xpv */
if (c == '\n') {
_doputchar(console, '\r');
if (diag != console)
_doputchar(diag, '\r');
}
_doputchar(console, c);
if (diag != console)
_doputchar(diag, c);
}
/*
* kernel character input functions
*/
int
bcons_getchar(void)
{
#if defined(__xpv)
if (!DOMAIN_IS_INITDOMAIN(xen_info) ||
console == CONS_HYPERVISOR)
return (bcons_getchar_xen());
#endif /* __xpv */
for (;;) {
if (console == CONS_TTY || diag == CONS_TTY) {
if (serial_ischar())
return (serial_getchar());
}
if (console != CONS_INVALID || diag != CONS_INVALID) {
if (kb_ischar())
return (kb_getchar());
}
}
}
/*
* Nothing below is used by dboot.
*/
#if !defined(_BOOT)
int
bcons_ischar(void)
{
int c = 0;
#if defined(__xpv)
if (!DOMAIN_IS_INITDOMAIN(xen_info) ||
console == CONS_HYPERVISOR)
return (bcons_ischar_xen());
#endif /* __xpv */
switch (console) {
case CONS_TTY:
c = serial_ischar();
break;
case CONS_INVALID:
break;
default:
c = kb_ischar();
}
if (c != 0)
return (c);
switch (diag) {
case CONS_TTY:
c = serial_ischar();
break;
case CONS_INVALID:
break;
default:
c = kb_ischar();
}
return (c);
}
/*
* 2nd part of console initialization: we've now processed bootenv.rc; update
* console settings as appropriate. This only really processes serial console
* modifications.
*/
void
bcons_post_bootenvrc(char *inputdev, char *outputdev, char *consoledev)
{
int cons = CONS_INVALID;
int ttyn;
char *devnames[] = { consoledev, outputdev, inputdev, NULL };
int i;
extern int post_fastreboot;
ttyn = 0;
if (post_fastreboot && console == CONS_SCREEN_GRAPHICS)
console = CONS_SCREEN_TEXT;
/*
* USB serial and GRAPHICS console: we just collect data into a buffer.
*/
if (console == CONS_USBSER || console == CONS_SCREEN_GRAPHICS) {
extern void *defcons_init(size_t);
defcons_buf = defcons_cur = defcons_init(MMU_PAGESIZE);
return;
}
for (i = 0; devnames[i] != NULL; i++) {
cons = lookup_console_device(devnames[i], &ttyn);
if (cons != CONS_INVALID)
break;
}
if (cons == CONS_INVALID) {
/*
* No console change, but let's see if bootenv.rc had a mode
* setting we should apply.
*/
if (console == CONS_TTY && !bootprop_set_tty_mode)
serial_init();
return;
}
#if defined(__xpv)
/*
* if the hypervisor is using the currently selected console device then
* default to using the hypervisor as the console device.
*/
if (cons == console_hypervisor_device) {
cons = CONS_HYPERVISOR;
console_hypervisor_redirect = B_TRUE;
}
#endif /* __xpv */
console = cons;
if (console == CONS_TTY) {
tty_num = ttyn;
serial_init();
}
}
#if defined(__xpv)
boolean_t
bcons_hypervisor_redirect(void)
{
return (console_hypervisor_redirect);
}
void
bcons_device_change(int new_console)
{
if (new_console < CONS_MIN || new_console > CONS_MAX)
return;
/*
* If we are asked to switch the console to the hypervisor, that
* really means to switch the console to whichever device the
* hypervisor is/was using.
*/
if (new_console == CONS_HYPERVISOR)
new_console = console_hypervisor_device;
console = new_console;
if (new_console == CONS_TTY) {
tty_num = console_hypervisor_tty_num;
serial_init();
}
}
#endif /* __xpv */
static void
defcons_putchar(int c)
{
if (defcons_buf != NULL &&
defcons_cur + 1 - defcons_buf < MMU_PAGESIZE) {
*defcons_cur++ = c;
*defcons_cur = 0;
}
}
#endif /* _BOOT */
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2016 Toomas Soome <tsome@me.com>
*/
#ifndef _BOOT_CONSOLE_IMPL_H
#define _BOOT_CONSOLE_IMPL_H
#include <sys/types.h>
#include <sys/bootinfo.h>
/*
* Boot console implementation details.
*/
#ifdef __cplusplus
extern "C" {
#endif
/* Console device callbacks. */
typedef struct bcons_dev {
void (*bd_putchar)(int);
void (*bd_eraseline)(void);
void (*bd_cursor)(boolean_t);
void (*bd_setpos)(int, int);
void (*bd_shift)(int);
} bcons_dev_t;
extern boolean_t xbi_fb_init(struct xboot_info *, bcons_dev_t *);
extern void boot_fb_init(int);
extern void boot_vga_init(bcons_dev_t *);
extern void boot_get_color(uint32_t *, uint32_t *);
#ifdef __cplusplus
}
#endif
#endif /* _BOOT_CONSOLE_IMPL_H */
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2016 Toomas Soome <tsoome@me.com>
*/
/*
* dboot and early kernel needs simple putchar(int) interface to implement
* printf() support. So we implement simple interface on top of
* linear frame buffer, since we can not use tem directly, we are
* just borrowing bits from it.
*
* Note, this implementation is assuming UEFI linear frame buffer and
* 32-bit depth, which should not be issue as GOP is supposed to provide those.
* At the time of writing, this is the only case for frame buffer anyhow.
*/
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/multiboot2.h>
#include <sys/framebuffer.h>
#include <sys/bootinfo.h>
#include <sys/boot_console.h>
#include <sys/bootconf.h>
#include <sys/rgb.h>
#include "boot_console_impl.h"
#define P2ROUNDUP(x, align) (-(-(x) & -(align)))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define nitems(x) (sizeof ((x)) / sizeof ((x)[0]))
/*
* Simplified visual_io data structures from visual_io.h
*/
struct vis_consdisplay {
uint16_t row; /* Row to display data at */
uint16_t col; /* Col to display data at */
uint16_t width; /* Width of data */
uint16_t height; /* Height of data */
uint8_t *data; /* Data to display */
};
struct vis_conscopy {
uint16_t s_row; /* Starting row */
uint16_t s_col; /* Starting col */
uint16_t e_row; /* Ending row */
uint16_t e_col; /* Ending col */
uint16_t t_row; /* Row to move to */
uint16_t t_col; /* Col to move to */
};
/*
* We have largest font 16x32 with depth 32. This will allocate 2048
* bytes from BSS.
*/
#define MAX_GLYPH (16 * 32 * 4)
struct fontlist cf_fontlist;
static bitmap_data_t cf_data;
static struct font cf_font;
static struct font boot_fb_font; /* set by set_font() */
static uint8_t glyph[MAX_GLYPH];
static void boot_fb_putchar(int);
static void boot_fb_eraseline(void);
static void boot_fb_setpos(int, int);
static void boot_fb_shiftline(int);
static void boot_fb_eraseline_impl(uint16_t, uint16_t);
static void
xbi_init_font(struct xboot_info *xbi)
{
uint32_t i, checksum = 0;
struct boot_modules *modules;
struct font_info *fi;
uintptr_t ptr;
modules = (struct boot_modules *)(uintptr_t)xbi->bi_modules;
for (i = 0; i < xbi->bi_module_cnt; i++) {
if (modules[i].bm_type == BMT_FONT)
break;
}
if (i == xbi->bi_module_cnt)
return;
ptr = (uintptr_t)modules[i].bm_addr;
fi = (struct font_info *)ptr;
/*
* Compute and verify checksum. The total sum of all the fields
* must be 0. Note, the return from this point means we will
* use default font.
*/
checksum += fi->fi_width;
checksum += fi->fi_height;
checksum += fi->fi_bitmap_size;
for (i = 0; i < VFNT_MAPS; i++)
checksum += fi->fi_map_count[i];
if (checksum + fi->fi_checksum != 0)
return;
cf_data.width = fi->fi_width;
cf_data.height = fi->fi_height;
cf_data.uncompressed_size = fi->fi_bitmap_size;
cf_data.font = &cf_font;
ptr += sizeof (struct font_info);
ptr = P2ROUNDUP(ptr, 8);
cf_font.vf_width = fi->fi_width;
cf_font.vf_height = fi->fi_height;
for (i = 0; i < VFNT_MAPS; i++) {
if (fi->fi_map_count[i] == 0)
continue;
cf_font.vf_map_count[i] = fi->fi_map_count[i];
cf_font.vf_map[i] = (struct font_map *)ptr;
ptr += (fi->fi_map_count[i] * sizeof (struct font_map));
ptr = P2ROUNDUP(ptr, 8);
}
cf_font.vf_bytes = (uint8_t *)ptr;
cf_fontlist.font_name = NULL;
cf_fontlist.font_flags = FONT_BOOT;
cf_fontlist.font_data = &cf_data;
cf_fontlist.font_load = NULL;
STAILQ_INSERT_HEAD(&fonts, &cf_fontlist, font_next);
}
/*
* extract data from MB2 framebuffer tag and set up initial frame buffer.
*/
boolean_t
xbi_fb_init(struct xboot_info *xbi, bcons_dev_t *bcons_dev)
{
multiboot_tag_framebuffer_t *tag;
boot_framebuffer_t *xbi_fb;
xbi_fb = (boot_framebuffer_t *)(uintptr_t)xbi->bi_framebuffer;
if (xbi_fb == NULL)
return (B_FALSE);
#if !defined(_BOOT)
/* For early kernel, we get cursor position from dboot. */
fb_info.cursor.origin.x = xbi_fb->cursor.origin.x;
fb_info.cursor.origin.y = xbi_fb->cursor.origin.y;
fb_info.cursor.pos.x = xbi_fb->cursor.pos.x;
fb_info.cursor.pos.y = xbi_fb->cursor.pos.y;
fb_info.cursor.visible = xbi_fb->cursor.visible;
#endif
xbi_init_font(xbi);
tag = (multiboot_tag_framebuffer_t *)(uintptr_t)xbi_fb->framebuffer;
if (tag == NULL) {
return (B_FALSE);
}
fb_info.paddr = tag->framebuffer_common.framebuffer_addr;
fb_info.pitch = tag->framebuffer_common.framebuffer_pitch;
fb_info.depth = tag->framebuffer_common.framebuffer_bpp;
fb_info.bpp = P2ROUNDUP(fb_info.depth, 8) >> 3;
fb_info.screen.x = tag->framebuffer_common.framebuffer_width;
fb_info.screen.y = tag->framebuffer_common.framebuffer_height;
fb_info.fb_size = fb_info.screen.y * fb_info.pitch;
bcons_dev->bd_putchar = boot_fb_putchar;
bcons_dev->bd_eraseline = boot_fb_eraseline;
bcons_dev->bd_cursor = boot_fb_cursor;
bcons_dev->bd_setpos = boot_fb_setpos;
bcons_dev->bd_shift = boot_fb_shiftline;
if (fb_info.paddr == 0)
fb_info.fb_type = FB_TYPE_UNKNOWN;
switch (tag->framebuffer_common.framebuffer_type) {
case MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT:
fb_info.fb_type = FB_TYPE_EGA_TEXT;
return (B_FALSE);
case MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED:
if (fb_info.paddr != 0)
fb_info.fb_type = FB_TYPE_INDEXED;
return (B_TRUE);
case MULTIBOOT_FRAMEBUFFER_TYPE_RGB:
if (fb_info.paddr != 0)
fb_info.fb_type = FB_TYPE_RGB;
break;
default:
return (B_FALSE);
}
fb_info.rgb.red.size = tag->u.fb2.framebuffer_red_mask_size;
fb_info.rgb.red.pos = tag->u.fb2.framebuffer_red_field_position;
fb_info.rgb.green.size = tag->u.fb2.framebuffer_green_mask_size;
fb_info.rgb.green.pos = tag->u.fb2.framebuffer_green_field_position;
fb_info.rgb.blue.size = tag->u.fb2.framebuffer_blue_mask_size;
fb_info.rgb.blue.pos = tag->u.fb2.framebuffer_blue_field_position;
rgb_info = fb_info.rgb;
return (B_TRUE);
}
/* set font and pass the data to fb_info */
static void
boot_fb_set_font(uint16_t height, uint16_t width)
{
short h, w;
bitmap_data_t *bp;
int i;
h = MIN(height, 4096);
w = MIN(width, 4096);
bp = set_font((short *)&fb_info.terminal.y,
(short *)&fb_info.terminal.x, h, w);
boot_fb_font.vf_bytes = bp->font->vf_bytes;
boot_fb_font.vf_width = bp->font->vf_width;
boot_fb_font.vf_height = bp->font->vf_height;
for (i = 0; i < VFNT_MAPS; i++) {
boot_fb_font.vf_map[i] = bp->font->vf_map[i];
boot_fb_font.vf_map_count[i] = bp->font->vf_map_count[i];
}
fb_info.font_width = boot_fb_font.vf_width;
fb_info.font_height = boot_fb_font.vf_height;
}
/* fill framebuffer */
static void
boot_fb_fill(uint8_t *dst, uint32_t data, uint32_t len)
{
uint16_t *dst16;
uint32_t *dst32;
uint32_t i;
switch (fb_info.depth) {
case 24:
case 8:
for (i = 0; i < len; i++)
dst[i] = (uint8_t)data;
break;
case 15:
case 16:
dst16 = (uint16_t *)dst;
len /= 2;
for (i = 0; i < len; i++)
dst16[i] = (uint16_t)data;
break;
case 32:
dst32 = (uint32_t *)dst;
len /= 4;
for (i = 0; i < len; i++)
dst32[i] = data;
break;
}
}
/* copy data to framebuffer */
static void
boot_fb_cpy(uint8_t *dst, uint8_t *src, uint32_t len)
{
uint16_t *dst16, *src16;
uint32_t *dst32, *src32;
switch (fb_info.depth) {
case 24:
case 8:
default:
if (dst <= src) {
do {
*dst++ = *src++;
} while (--len != 0);
} else {
dst += len;
src += len;
do {
*--dst = *--src;
} while (--len != 0);
}
break;
case 15:
case 16:
dst16 = (uint16_t *)dst;
src16 = (uint16_t *)src;
len /= 2;
if (dst16 <= src16) {
do {
*dst16++ = *src16++;
} while (--len != 0);
} else {
dst16 += len;
src16 += len;
do {
*--dst16 = *--src16;
} while (--len != 0);
}
break;
case 32:
dst32 = (uint32_t *)dst;
src32 = (uint32_t *)src;
len /= 4;
if (dst32 <= src32) {
do {
*dst32++ = *src32++;
} while (--len != 0);
} else {
dst32 += len;
src32 += len;
do {
*--dst32 = *--src32;
} while (--len != 0);
}
break;
}
}
/*
* Allocate shadow frame buffer, called from fakebop.c when early boot
* allocator is ready.
*/
void
boot_fb_shadow_init(bootops_t *bops)
{
if (boot_console_type(NULL) != CONS_FRAMEBUFFER)
return; /* nothing to do */
fb_info.shadow_fb = (uint8_t *)bops->bsys_alloc(NULL, NULL,
fb_info.fb_size, MMU_PAGESIZE);
if (fb_info.shadow_fb == NULL)
return;
/* Copy FB to shadow */
boot_fb_cpy(fb_info.shadow_fb, fb_info.fb, fb_info.fb_size);
}
/*
* Translate ansi color based on inverses and brightness.
*/
void
boot_get_color(uint32_t *fg, uint32_t *bg)
{
/* ansi to solaris colors, see also boot_console.c */
if (fb_info.inverse == B_TRUE ||
fb_info.inverse_screen == B_TRUE) {
if (fb_info.fg_color < XLATE_NCOLORS) {
/*
* white fg -> bright white bg
*/
if (fb_info.fg_color == pc_white)
*bg = brt_xlate[fb_info.fg_color];
else
*bg = dim_xlate[fb_info.fg_color];
} else {
*bg = fb_info.fg_color;
}
if (fb_info.bg_color < XLATE_NCOLORS) {
if (fb_info.bg_color == pc_white)
*fg = brt_xlate[fb_info.bg_color];
else
*fg = dim_xlate[fb_info.bg_color];
} else {
*fg = fb_info.bg_color;
}
} else {
if (fb_info.fg_color < XLATE_NCOLORS) {
if (fb_info.fg_color == pc_white)
*fg = brt_xlate[fb_info.fg_color];
else
*fg = dim_xlate[fb_info.fg_color];
} else {
*fg = fb_info.fg_color;
}
if (fb_info.bg_color < XLATE_NCOLORS) {
if (fb_info.bg_color == pc_white)
*bg = brt_xlate[fb_info.bg_color];
else
*bg = dim_xlate[fb_info.bg_color];
} else {
*bg = fb_info.bg_color;
}
}
}
/*
* Map indexed color to RGB value.
*/
uint32_t
boot_color_map(uint8_t index)
{
if (fb_info.fb_type != FB_TYPE_RGB) {
if (index < nitems(solaris_color_to_pc_color))
return (solaris_color_to_pc_color[index]);
else
return (index);
}
return (rgb_color_map(&fb_info.rgb, index, 0));
}
/* set up out simple console. */
/*ARGSUSED*/
void
boot_fb_init(int console)
{
fb_info_pixel_coord_t window;
/* frame buffer address is mapped in dboot. */
fb_info.fb = (uint8_t *)(uintptr_t)fb_info.paddr;
boot_fb_set_font(fb_info.screen.y, fb_info.screen.x);
window.x = (fb_info.screen.x -
fb_info.terminal.x * boot_fb_font.vf_width) / 2;
window.y = (fb_info.screen.y -
fb_info.terminal.y * boot_fb_font.vf_height) / 2;
fb_info.terminal_origin.x = window.x;
fb_info.terminal_origin.y = window.y;
#if defined(_BOOT)
/*
* Being called from dboot, we can have cursor terminal
* position passed from boot loader. In such case, fix the
* cursor screen coords.
*/
if (fb_info.cursor.pos.x != 0 || fb_info.cursor.pos.y != 0) {
fb_info.cursor.origin.x = window.x +
fb_info.cursor.pos.x * boot_fb_font.vf_width;
fb_info.cursor.origin.y = window.y +
fb_info.cursor.pos.y * boot_fb_font.vf_height;
}
#endif
/* If the cursor terminal position is 0,0 just reset screen coords */
if (fb_info.cursor.pos.x == 0 && fb_info.cursor.pos.y == 0) {
fb_info.cursor.origin.x = window.x;
fb_info.cursor.origin.y = window.y;
}
/*
* Validate cursor coords with screen/terminal dimensions,
* if anything is off, reset to 0,0
*/
if (fb_info.cursor.pos.x > fb_info.terminal.x ||
fb_info.cursor.pos.y > fb_info.terminal.y ||
fb_info.cursor.origin.x > fb_info.screen.x ||
fb_info.cursor.origin.y > fb_info.screen.y) {
fb_info.cursor.origin.x = window.x;
fb_info.cursor.origin.y = window.y;
fb_info.cursor.pos.x = 0;
fb_info.cursor.pos.y = 0;
}
#if defined(_BOOT)
/* clear the screen if cursor is set to 0,0 */
if (fb_info.cursor.pos.x == 0 && fb_info.cursor.pos.y == 0) {
uint32_t fg, bg, toffset;
uint16_t y;
boot_get_color(&fg, &bg);
bg = boot_color_map(bg);
toffset = 0;
for (y = 0; y < fb_info.screen.y; y++) {
uint8_t *dest = fb_info.fb + toffset;
boot_fb_fill(dest, bg, fb_info.pitch);
toffset += fb_info.pitch;
}
}
#endif
}
/* copy rectangle to framebuffer. */
static void
boot_fb_blit(struct vis_consdisplay *rect)
{
uint32_t offset, size; /* write size per scanline */
uint8_t *fbp, *sfbp = NULL; /* fb + calculated offset */
int i;
/* make sure we will not write past FB */
if (rect->col >= fb_info.screen.x ||
rect->row >= fb_info.screen.y ||
rect->col + rect->width >= fb_info.screen.x ||
rect->row + rect->height >= fb_info.screen.y)
return;
size = rect->width * fb_info.bpp;
offset = rect->col * fb_info.bpp + rect->row * fb_info.pitch;
fbp = fb_info.fb + offset;
if (fb_info.shadow_fb != NULL)
sfbp = fb_info.shadow_fb + offset;
/* write all scanlines in rectangle */
for (i = 0; i < rect->height; i++) {
uint8_t *dest = fbp + i * fb_info.pitch;
uint8_t *src = rect->data + i * size;
boot_fb_cpy(dest, src, size);
if (sfbp != NULL) {
dest = sfbp + i * fb_info.pitch;
boot_fb_cpy(dest, src, size);
}
}
}
static void
bit_to_pix(uchar_t c)
{
uint32_t fg, bg;
boot_get_color(&fg, &bg);
fg = boot_color_map(fg);
bg = boot_color_map(bg);
switch (fb_info.depth) {
case 8:
font_bit_to_pix8(&boot_fb_font, (uint8_t *)glyph, c, fg, bg);
break;
case 15:
case 16:
font_bit_to_pix16(&boot_fb_font, (uint16_t *)glyph, c,
(uint16_t)fg, (uint16_t)bg);
break;
case 24:
font_bit_to_pix24(&boot_fb_font, (uint8_t *)glyph, c, fg, bg);
break;
case 32:
font_bit_to_pix32(&boot_fb_font, (uint32_t *)glyph, c, fg, bg);
break;
}
}
static void
boot_fb_eraseline_impl(uint16_t x, uint16_t y)
{
uint32_t toffset, size;
uint32_t fg, bg;
uint8_t *dst, *sdst;
int i;
boot_get_color(&fg, &bg);
bg = boot_color_map(bg);
size = fb_info.terminal.x * boot_fb_font.vf_width * fb_info.bpp;
toffset = x * fb_info.bpp + y * fb_info.pitch;
dst = fb_info.fb + toffset;
sdst = fb_info.shadow_fb + toffset;
for (i = 0; i < boot_fb_font.vf_height; i++) {
uint8_t *dest = dst + i * fb_info.pitch;
if (fb_info.fb + fb_info.fb_size >= dest + size)
boot_fb_fill(dest, bg, size);
if (fb_info.shadow_fb != NULL) {
dest = sdst + i * fb_info.pitch;
if (fb_info.shadow_fb + fb_info.fb_size >=
dest + size) {
boot_fb_fill(dest, bg, size);
}
}
}
}
static void
boot_fb_eraseline(void)
{
boot_fb_eraseline_impl(fb_info.cursor.origin.x,
fb_info.cursor.origin.y);
}
/*
* Copy rectangle from console to console.
* If shadow buffer is available, use shadow as source.
*/
static void
boot_fb_conscopy(struct vis_conscopy *c_copy)
{
uint32_t soffset, toffset;
uint32_t width, height, increment;
uint8_t *src, *dst, *sdst = NULL;
int i;
soffset = c_copy->s_col * fb_info.bpp + c_copy->s_row * fb_info.pitch;
toffset = c_copy->t_col * fb_info.bpp + c_copy->t_row * fb_info.pitch;
src = fb_info.fb + soffset;
dst = fb_info.fb + toffset;
if (fb_info.shadow_fb != NULL) {
src = fb_info.shadow_fb + soffset;
sdst = fb_info.shadow_fb + toffset;
}
width = (c_copy->e_col - c_copy->s_col + 1) * fb_info.bpp;
height = c_copy->e_row - c_copy->s_row + 1;
for (i = 0; i < height; i++) {
increment = i * fb_info.pitch;
/* Make sure we fit into FB size. */
if (soffset + increment + width >= fb_info.fb_size ||
toffset + increment + width >= fb_info.fb_size)
break;
boot_fb_cpy(dst + increment, src + increment, width);
if (sdst != NULL)
boot_fb_cpy(sdst + increment, src + increment, width);
}
}
/* Shift the line content by chars. */
static void
boot_fb_shiftline(int chars)
{
struct vis_conscopy c_copy;
c_copy.s_col = fb_info.cursor.origin.x;
c_copy.s_row = fb_info.cursor.origin.y;
c_copy.e_col = (fb_info.terminal.x - chars) * boot_fb_font.vf_width;
c_copy.e_col += fb_info.terminal_origin.x;
c_copy.e_row = c_copy.s_row + boot_fb_font.vf_height;
c_copy.t_col = fb_info.cursor.origin.x + chars * boot_fb_font.vf_width;
c_copy.t_row = fb_info.cursor.origin.y;
boot_fb_conscopy(&c_copy);
}
/*
* move the terminal window lines [1..y] to [0..y-1] and clear last line.
*/
static void
boot_fb_scroll(void)
{
struct vis_conscopy c_copy;
/* support for scrolling. set up the console copy data and last line */
c_copy.s_row = fb_info.terminal_origin.y + boot_fb_font.vf_height;
c_copy.s_col = fb_info.terminal_origin.x;
c_copy.e_row = fb_info.screen.y - fb_info.terminal_origin.y;
c_copy.e_col = fb_info.screen.x - fb_info.terminal_origin.x;
c_copy.t_row = fb_info.terminal_origin.y;
c_copy.t_col = fb_info.terminal_origin.x;
boot_fb_conscopy(&c_copy);
/* now clean up the last line */
boot_fb_eraseline_impl(fb_info.terminal_origin.x,
fb_info.terminal_origin.y +
(fb_info.terminal.y - 1) * boot_fb_font.vf_height);
}
/*
* Very simple block cursor. Save space below the cursor and restore
* when cursor is invisible.
*/
void
boot_fb_cursor(boolean_t visible)
{
uint32_t offset, size, j;
uint32_t *fb32, *sfb32 = NULL;
uint32_t fg, bg;
uint16_t *fb16, *sfb16 = NULL;
uint8_t *fb8, *sfb8 = NULL;
int i, pitch;
if (fb_info.cursor.visible == visible)
return;
boot_get_color(&fg, &bg);
fg = boot_color_map(fg);
bg = boot_color_map(bg);
fb_info.cursor.visible = visible;
pitch = fb_info.pitch;
size = boot_fb_font.vf_width * fb_info.bpp;
/*
* Build cursor image. We are building mirror image of data on
* frame buffer by (D xor FG) xor BG.
*/
offset = fb_info.cursor.origin.x * fb_info.bpp +
fb_info.cursor.origin.y * pitch;
switch (fb_info.depth) {
case 8:
for (i = 0; i < boot_fb_font.vf_height; i++) {
fb8 = fb_info.fb + offset + i * pitch;
if (fb_info.shadow_fb != NULL)
sfb8 = fb_info.shadow_fb + offset + i * pitch;
for (j = 0; j < size; j += 1) {
fb8[j] = (fb8[j] ^ (fg & 0xff)) ^ (bg & 0xff);
if (sfb8 == NULL)
continue;
sfb8[j] = (sfb8[j] ^ (fg & 0xff)) ^ (bg & 0xff);
}
}
break;
case 15:
case 16:
for (i = 0; i < boot_fb_font.vf_height; i++) {
fb16 = (uint16_t *)(fb_info.fb + offset + i * pitch);
if (fb_info.shadow_fb != NULL)
sfb16 = (uint16_t *)
(fb_info.shadow_fb + offset + i * pitch);
for (j = 0; j < boot_fb_font.vf_width; j++) {
fb16[j] = (fb16[j] ^ (fg & 0xffff)) ^
(bg & 0xffff);
if (sfb16 == NULL)
continue;
sfb16[j] = (sfb16[j] ^ (fg & 0xffff)) ^
(bg & 0xffff);
}
}
break;
case 24:
for (i = 0; i < boot_fb_font.vf_height; i++) {
fb8 = fb_info.fb + offset + i * pitch;
if (fb_info.shadow_fb != NULL)
sfb8 = fb_info.shadow_fb + offset + i * pitch;
for (j = 0; j < size; j += 3) {
fb8[j] = (fb8[j] ^ ((fg >> 16) & 0xff)) ^
((bg >> 16) & 0xff);
fb8[j+1] = (fb8[j+1] ^ ((fg >> 8) & 0xff)) ^
((bg >> 8) & 0xff);
fb8[j+2] = (fb8[j+2] ^ (fg & 0xff)) ^
(bg & 0xff);
if (sfb8 == NULL)
continue;
sfb8[j] = (sfb8[j] ^ ((fg >> 16) & 0xff)) ^
((bg >> 16) & 0xff);
sfb8[j+1] = (sfb8[j+1] ^ ((fg >> 8) & 0xff)) ^
((bg >> 8) & 0xff);
sfb8[j+2] = (sfb8[j+2] ^ (fg & 0xff)) ^
(bg & 0xff);
}
}
break;
case 32:
for (i = 0; i < boot_fb_font.vf_height; i++) {
fb32 = (uint32_t *)(fb_info.fb + offset + i * pitch);
if (fb_info.shadow_fb != NULL) {
sfb32 = (uint32_t *)
(fb_info.shadow_fb + offset + i * pitch);
}
for (j = 0; j < boot_fb_font.vf_width; j++) {
fb32[j] = (fb32[j] ^ fg) ^ bg;
if (sfb32 == NULL)
continue;
sfb32[j] = (sfb32[j] ^ fg) ^ bg;
}
}
break;
}
}
static void
boot_fb_setpos(int row, int col)
{
if (row < 0)
row = 0;
if (row >= fb_info.terminal.y)
row = fb_info.terminal.y - 1;
if (col < 0)
col = 0;
if (col >= fb_info.terminal.x)
col = fb_info.terminal.x - 1;
fb_info.cursor.pos.x = col;
fb_info.cursor.pos.y = row;
fb_info.cursor.origin.x = fb_info.terminal_origin.x;
fb_info.cursor.origin.x += col * boot_fb_font.vf_width;
fb_info.cursor.origin.y = fb_info.terminal_origin.y;
fb_info.cursor.origin.y += row * boot_fb_font.vf_height;
}
static void
boot_fb_putchar(int c)
{
struct vis_consdisplay display;
int rows, cols;
rows = fb_info.cursor.pos.y;
cols = fb_info.cursor.pos.x;
if (c == '\n') {
if (rows < fb_info.terminal.y - 1)
boot_fb_setpos(rows + 1, cols);
else
boot_fb_scroll();
return;
}
bit_to_pix(c);
display.col = fb_info.cursor.origin.x;
display.row = fb_info.cursor.origin.y;
display.width = boot_fb_font.vf_width;
display.height = boot_fb_font.vf_height;
display.data = glyph;
boot_fb_blit(&display);
if (cols < fb_info.terminal.x - 1)
boot_fb_setpos(rows, cols + 1);
else if (rows < fb_info.terminal.y - 1)
boot_fb_setpos(rows + 1, 0);
else {
boot_fb_setpos(rows, 0);
boot_fb_scroll();
}
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Copyright 2020 Joyent, Inc.
* Copyright 2022 Oxide Computer Company
*/
/*
* The boot GDT must remain in sync with the entries in intel/sys/segments.h; in
* particular kmdb uses B64CODE_SEL or B32CODE_SEL in perpetuity for its IDT
* entries (they're copied to the kernel's GDT in init_idt()).
*
* The GDT is effectively an array of user_desc_t entries.
*/
.align 16
.data
global_descriptor_table:
.long 0
.long 0
/* GDT_B32DATA: 32 bit flat data descriptor */
.value 0xFFFF /* segment limit 0..15 */
.value 0x0000 /* segment base 0..15 */
.byte 0x0 /* segment base 16..23 */
.byte 0x92 /* P = 1, read/write data */
.byte 0xCF /* G=1, B=1, Limit (16..19)=1111 */
.byte 0x0 /* segment base 24..32 */
/* GDT_B32CODE 32 bit flat code descriptor */
.value 0xFFFF /* segment limit 0..15 */
.value 0x0000 /* segment base 0..15 */
.byte 0x0 /* segment base 16..23 */
.byte 0x9A /* P=1, code, exec, readable */
.byte 0xCF /* G=1, D=1, Limit (16..19)=1111 */
.byte 0x0 /* segment base 24..32 */
/*
* GDT_B16CODE 16 bit code descriptor for doing BIOS calls
*/
.value 0xFFFF /* segment limit 0..15 */
.value 0x0000 /* segment base 0..15 */
.byte 0x0 /* segment base 16..23 */
.byte 0x9A /* P=1, code, exec, readable */
.byte 0x0F /* G=0, D=0, Limit (16..19)=1111 */
.byte 0x0 /* segment base 24..32 */
/*
* GDT_B16DATA 16 bit data descriptor for doing BIOS calls
* XXX: Note that this sets the B flag, which is not supposed to be
* set for a 16-bit data segment that is not expand-down (Intel SDM
* vol 3A sec 3.4.5). AMD does not seem to care (AMD APM vol 2 sec
* 2.7.4). It is likely this is here for use with stack segments,
* where it effects 32-bit operations. Conceivably, we could be more
* conservative and specify a 16-bit stack segment, but leave it for
* now.
*/
.value 0xFFFF /* segment limit 0..15 */
.value 0x0000 /* segment base 0..15 */
.byte 0x0 /* segment base 16..23 */
.byte 0x92 /* P = 1, read/write data */
.byte 0x4F /* G=0, B=1, Limit (16..19)=1111 */
.byte 0x0 /* segment base 24..32 */
/* GDT_B64CODE: 64 bit flat code descriptor - only L bit has meaning */
.value 0xFFFF /* segment limit 0..15 */
.value 0x0000 /* segment base 0..15 */
.byte 0x0 /* segment base 16..23 */
.byte 0x9A /* P=1, code, exec, readable */
.byte 0xAF /* G=1, D=0, L=1, Limit (16..19)=1111 */
.byte 0x0 /* segment base 24..32 */
/*
* unused
*/
.long 0
.long 0
/*
* GDT_BGSTMP -- an entry for kmdb to use during boot
* the fast reboot code uses this entry for memory copies, too.
*/
.value 0x0001 /* segment limit 0..15 */
.globl fake_cpu_gdt_base_0_15
fake_cpu_gdt_base_0_15:
.value 0x0000 /* segment base 0..15 */
.globl fake_cpu_gdt_base_16_23
fake_cpu_gdt_base_16_23:
.byte 0x0 /* segment base 16..23 */
.byte 0x9A /* P=1, code, exec, readable */
.byte 0xC0 /* G=1, D=1, Limit (16..19)=0000 */
.globl fake_cpu_gdt_base_24_31
fake_cpu_gdt_base_24_31:
.byte 0x0 /* segment base 24..32 */
/ .long 0
/ .long 0
/*
* This is a desctbr_t.
*/
gdt_info:
.value gdt_info - global_descriptor_table - 1
.long global_descriptor_table
.long 0 /* needed for 64 bit */
fake_cpu:
.4byte 0
.4byte 0
.4byte 0
.globl fake_cpu_ptr
fake_cpu_ptr:
.4byte 0
.skip 0x6c0, 0
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Miniature keyboard driver for bootstrap. This allows keyboard
* support to continue after we take over interrupts and disable
* BIOS keyboard support.
*/
#include <sys/types.h>
#include <sys/archsystm.h>
#include <sys/boot_console.h>
#include "boot_keyboard_table.h"
#if defined(_BOOT)
#include "dboot/dboot_asm.h"
#include "dboot/dboot_xboot.h"
#endif /* _BOOT */
/*
* Definitions for BIOS keyboard state. We use BIOS's variable to store
* state, ensuring that we stay in sync with it.
*/
#define BIOS_KB_FLAG 0x417
#define BIOS_RIGHT_SHIFT 0x01
#define BIOS_LEFT_SHIFT 0x02
#define BIOS_EITHER_SHIFT (BIOS_LEFT_SHIFT | BIOS_RIGHT_SHIFT)
#define BIOS_CTL_SHIFT 0x04
#define BIOS_ALT_SHIFT 0x08
#define BIOS_SCROLL_STATE 0x10
#define BIOS_NUM_STATE 0x20
#define BIOS_CAPS_STATE 0x40
#define BIOS_INS_STATE 0x80
#define BIOS_KB_FLAG_1 0x418
#define BIOS_SYS_SHIFT 0x04
#define BIOS_HOLD_STATE 0x08
#define BIOS_SCROLL_SHIFT 0x10
#define BIOS_NUM_SHIFT 0x20
#define BIOS_CAPS_SHIFT 0x40
#define BIOS_INS_SHIFT 0x80
#if defined(__xpv) && defined(_BOOT)
/*
* Device memory addresses
*
* In dboot under the hypervisor we don't have any memory mappings
* for the first meg of low memory so we can't access devices there.
* Intead we've mapped the device memory that we need to access into
* a local variable within dboot so we can access the device memory
* there.
*/
extern unsigned short *kb_status;
#define kb_flag ((unsigned char *)&kb_status[BIOS_KB_FLAG])
#define kb_flag_1 ((unsigned char *)&kb_status[BIOS_KB_FLAG_1])
#else /* __xpv && _BOOT */
/* Device memory addresses */
static unsigned char *kb_status = ((unsigned char *)BIOS_KB_FLAG);
#define kb_flag (&kb_status[0])
#define kb_flag_1 (&kb_status[1])
#endif /* __xpv && _BOOT */
/*
* Keyboard controller registers
*/
#define I8042_DATA 0x60
#define I8042_STAT 0x64
#define I8042_CMD 0x64
/*
* Keyboard controller status register bits
*/
#define I8042_STAT_OUTBF 0x01
#define I8042_STAT_INBF 0x02
#define I8042_STAT_AUXBF 0x20
/*
* Keyboard controller commands
*/
#define I8042_RCB 0x20
#define I8042_WCB 0x60
/*
* Keyboard commands
*/
#define KB_SET_LED 0xED /* LED byte follows... */
#define KB_LED_SCROLL_LOCK 0x01 /* Bits for LED byte */
#define KB_LED_NUM_LOCK 0x02
#define KB_LED_CAPS_LOCK 0x04
#ifndef ASSERT
#define ASSERT(x)
#endif
#define peek8(p) (*(p))
#define poke8(p, val) (*(p) = (val))
static struct {
boolean_t initialized;
enum { KB_LED_IDLE, KB_LED_COMMAND_SENT, KB_LED_VALUE_SENT }
led_state;
int led_commanded;
/*
* Possible values:
*
* -1 Nothing pending
* 0x000-0x0ff Pending byte
* 0x100-0x1ff Needs leading zero, then low byte next.
*
* Others undefined.
*/
int pending;
} kb = {
B_FALSE, /* initialized? */
KB_LED_IDLE, /* LED command state */
-1, /* commanded LEDs - force refresh */
-1, /* pending */
};
#define KTAB_STRLEN 3
static char keystringtab[KTAB_STRLEN] = {'\033', '[', ' '};
static int keystring = -1;
static int kb_translate(unsigned char code);
static void kb_send(unsigned char cmd);
static void kb_update_leds(void);
static uchar_t kb_calculate_leds(void);
int
kb_getchar(void)
{
int ret;
while (!kb_ischar())
/* LOOP */;
if (keystring >= 0) {
ret = keystringtab[keystring++];
if (keystring == KTAB_STRLEN) {
keystring = -1;
kb.pending = -1;
}
return (ret);
}
/*
* kb_ischar() doesn't succeed without leaving kb.pending
* set.
*/
ASSERT(kb.pending >= 0);
if (kb.pending & 0x100) {
kb.pending &= 0xff;
switch (kb.pending) {
case 'H': /* Up */
keystringtab[2] = 'A';
keystring = 0;
return (kb_getchar());
case 'P': /* Down */
keystringtab[2] = 'B';
keystring = 0;
return (kb_getchar());
case 'M': /* Right */
keystringtab[2] = 'C';
keystring = 0;
return (kb_getchar());
case 'K': /* Left */
keystringtab[2] = 'D';
keystring = 0;
return (kb_getchar());
default:
ret = 0;
}
} else {
ret = kb.pending;
kb.pending = -1;
}
return (ret);
}
int
kb_ischar(void)
{
unsigned char buffer_stat;
unsigned char code;
unsigned char leds;
if (!kb.initialized) {
kb_init();
kb.initialized = B_TRUE;
}
if (kb.pending >= 0)
return (1);
for (;;) {
buffer_stat = inb(I8042_STAT);
if (buffer_stat == 0xff)
return (0);
buffer_stat &= (I8042_STAT_OUTBF | I8042_STAT_AUXBF);
switch (buffer_stat) {
case 0:
case I8042_STAT_AUXBF:
return (0);
case (I8042_STAT_OUTBF | I8042_STAT_AUXBF):
/*
* Discard unwanted mouse data.
*/
(void) inb(I8042_DATA);
continue;
}
code = inb(I8042_DATA);
switch (code) {
/*
* case 0xAA:
*
* You might think that we should ignore 0xAA on the
* grounds that it is the BAT Complete response and will
* occur on keyboard detach/reattach. Unfortunately,
* it is ambiguous - this is also the code for a break
* of the left shift key. Since it will be harmless for
* us to "spuriously" process a break of Left Shift,
* we just let the normal code handle it. Perhaps we
* should take a hint and refresh the LEDs, but I
* refuse to get very worried about hot-plug issues
* in this mini-driver.
*/
case 0xFA:
switch (kb.led_state) {
case KB_LED_IDLE:
/*
* Spurious. Oh well, ignore it.
*/
break;
case KB_LED_COMMAND_SENT:
leds = kb_calculate_leds();
kb_send(leds);
kb.led_commanded = leds;
kb.led_state = KB_LED_VALUE_SENT;
break;
case KB_LED_VALUE_SENT:
kb.led_state = KB_LED_IDLE;
/*
* Check for changes made while we were
* working on the last change.
*/
kb_update_leds();
break;
}
continue;
case 0xE0:
case 0xE1:
/*
* These are used to distinguish the keys added on
* the AT-101 keyboard from the original 84 keys.
* We don't care, and the codes are carefully arranged
* so that we don't have to.
*/
continue;
default:
if (code & 0x80) {
/* Release */
code &= 0x7f;
switch (keyboard_translate[code].normal) {
case KBTYPE_SPEC_LSHIFT:
poke8(kb_flag, peek8(kb_flag) &
~BIOS_LEFT_SHIFT);
break;
case KBTYPE_SPEC_RSHIFT:
poke8(kb_flag, peek8(kb_flag) &
~BIOS_RIGHT_SHIFT);
break;
case KBTYPE_SPEC_CTRL:
poke8(kb_flag, peek8(kb_flag) &
~BIOS_CTL_SHIFT);
break;
case KBTYPE_SPEC_ALT:
poke8(kb_flag, peek8(kb_flag) &
~BIOS_ALT_SHIFT);
break;
case KBTYPE_SPEC_CAPS_LOCK:
poke8(kb_flag_1, peek8(kb_flag_1) &
~BIOS_CAPS_SHIFT);
break;
case KBTYPE_SPEC_NUM_LOCK:
poke8(kb_flag_1, peek8(kb_flag_1) &
~BIOS_NUM_SHIFT);
break;
case KBTYPE_SPEC_SCROLL_LOCK:
poke8(kb_flag_1, peek8(kb_flag_1) &
~BIOS_SCROLL_SHIFT);
break;
default:
/*
* Ignore all other releases.
*/
break;
}
} else {
/* Press */
kb.pending = kb_translate(code);
if (kb.pending >= 0) {
return (1);
}
}
}
}
}
int
kb_translate(unsigned char code)
{
struct keyboard_translate *k;
unsigned short action;
boolean_t shifted;
k = keyboard_translate + code;
shifted = (peek8(kb_flag) & BIOS_EITHER_SHIFT) != 0;
switch (k->normal & 0xFF00) {
case KBTYPE_NUMPAD:
if (peek8(kb_flag) & BIOS_NUM_STATE)
shifted = !shifted;
break;
case KBTYPE_ALPHA:
if (peek8(kb_flag) & BIOS_CAPS_STATE)
shifted = !shifted;
break;
}
if (peek8(kb_flag) & BIOS_ALT_SHIFT)
action = k->alted;
else if (peek8(kb_flag) & BIOS_CTL_SHIFT)
action = k->ctrled;
else if (shifted)
action = k->shifted;
else
action = k->normal;
switch (action & 0xFF00) {
case KBTYPE_NORMAL:
case KBTYPE_ALPHA:
return (action & 0xFF);
case KBTYPE_NUMPAD:
case KBTYPE_FUNC:
return ((action & 0xFF) | 0x100);
case KBTYPE_SPEC:
break;
default:
/*
* Bad entry.
*/
ASSERT(0);
return (-1);
}
/*
* Handle special keys, mostly shifts.
*/
switch (action) {
case KBTYPE_SPEC_NOP:
case KBTYPE_SPEC_UNDEF:
break;
case KBTYPE_SPEC_LSHIFT:
poke8(kb_flag, peek8(kb_flag) | BIOS_LEFT_SHIFT);
break;
case KBTYPE_SPEC_RSHIFT:
poke8(kb_flag, peek8(kb_flag) | BIOS_RIGHT_SHIFT);
break;
case KBTYPE_SPEC_CTRL:
poke8(kb_flag, peek8(kb_flag) | BIOS_CTL_SHIFT);
break;
case KBTYPE_SPEC_ALT:
poke8(kb_flag, peek8(kb_flag) | BIOS_ALT_SHIFT);
break;
case KBTYPE_SPEC_CAPS_LOCK:
if (!(peek8(kb_flag_1) & BIOS_CAPS_SHIFT)) {
poke8(kb_flag_1, peek8(kb_flag_1) | BIOS_CAPS_SHIFT);
poke8(kb_flag, peek8(kb_flag) ^ BIOS_CAPS_STATE);
}
break;
case KBTYPE_SPEC_NUM_LOCK:
if (!(peek8(kb_flag_1) & BIOS_NUM_SHIFT)) {
poke8(kb_flag_1, peek8(kb_flag_1) | BIOS_NUM_SHIFT);
poke8(kb_flag, peek8(kb_flag) ^ BIOS_NUM_STATE);
}
break;
case KBTYPE_SPEC_SCROLL_LOCK:
if (!(peek8(kb_flag_1) & BIOS_SCROLL_SHIFT)) {
poke8(kb_flag_1, peek8(kb_flag_1) | BIOS_SCROLL_SHIFT);
poke8(kb_flag, peek8(kb_flag) ^ BIOS_SCROLL_STATE);
}
break;
case KBTYPE_SPEC_MAYBE_REBOOT:
#if 0 /* Solaris doesn't reboot via ctrl-alt-del */
if ((peek8(kb_flag) & (BIOS_CTL_SHIFT|BIOS_ALT_SHIFT)) ==
(BIOS_CTL_SHIFT|BIOS_ALT_SHIFT)) {
reset();
/* NOTREACHED */
}
#endif
break;
default:
/*
* Bad entry
*/
ASSERT(0);
break;
}
/*
* Consider updating the LEDs. This does nothing if nothing
* needs to be done.
*/
kb_update_leds();
return (-1);
}
void
kb_send(unsigned char cmd)
{
int retries;
for (retries = 0;
(inb(I8042_STAT) & I8042_STAT_INBF) != 0 && retries < 100000;
retries++)
/* LOOP */;
outb(I8042_DATA, cmd);
}
void
kb_update_leds(void)
{
if (kb.led_state != KB_LED_IDLE) {
/*
* The state machine will take care of any additional
* changes that are necessary.
*/
return;
}
if (kb_calculate_leds() == kb.led_commanded) {
kb.led_state = KB_LED_IDLE;
} else {
kb_send(KB_SET_LED);
kb.led_state = KB_LED_COMMAND_SENT;
}
}
#define MIMR_PORT 0x21 /* Mask register for master PIC */
#define MIMR_KB 2 /* Keyboard mask bit in master PIC */
void
kb_init(void)
{
/*
* Resist the urge to muck with the keyboard/mouse. Just assume
* that the bios, grub, and any optional hypervisor have left
* the keyboard in a sane and usable state. Messing with it now
* could result it making it unusuable, which would break early
* kmdb debugging support. Note that we don't actually need to
* disable interrupts for the keyboard/mouse since we're already
* in protected mode and we're not compeating with the bios for
* keyboard access. Also, we don't need to disable the mouse
* port since our polled input routine will just drop any mouse
* data that it recieves.
*/
kb_update_leds();
}
unsigned char
kb_calculate_leds(void)
{
int res;
res = 0;
if (peek8(kb_flag) & BIOS_CAPS_STATE)
res |= KB_LED_CAPS_LOCK;
if (peek8(kb_flag) & BIOS_NUM_STATE)
res |= KB_LED_NUM_LOCK;
if (peek8(kb_flag) & BIOS_SCROLL_STATE)
res |= KB_LED_SCROLL_LOCK;
return ((char)res);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Keyboard table for bootstrap's simple keyboard driver.
*/
#include "boot_keyboard_table.h"
#define A | KBTYPE_ALPHA
#define C & 0x1f
#define F | KBTYPE_FUNC
#define N | KBTYPE_NUMPAD
#define ALT KBTYPE_SPEC_ALT
#define CTRL KBTYPE_SPEC_CTRL
#define LSHIFT KBTYPE_SPEC_LSHIFT
#define NOP KBTYPE_SPEC_NOP
#define NUMLK KBTYPE_SPEC_NUM_LOCK
#define SCRLLK KBTYPE_SPEC_SCROLL_LOCK
#define CAPSLK KBTYPE_SPEC_CAPS_LOCK
#define RSHIFT KBTYPE_SPEC_RSHIFT
#define REBOOT KBTYPE_SPEC_MAYBE_REBOOT
#define UNDEF KBTYPE_SPEC_UNDEF
struct keyboard_translate keyboard_translate[128] = {
/* Normal Shifted Ctrled Alted */
/* 00 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 01 */ '['C, '['C, NOP, NOP,
/* 02 */ '1', '!', NOP, 0x78 F,
/* 03 */ '2', '@', NOP, 0x79 F,
/* 04 */ '3', '#', NOP, 0x7a F,
/* 05 */ '4', '$', NOP, 0x7b F,
/* 06 */ '5', '%', NOP, 0x7c F,
/* 07 */ '6', '^', '^'C, 0x7d F,
/* 08 */ '7', '&', NOP, 0x7e F,
/* 09 */ '8', '*', NOP, 0x7f F,
/* 0a */ '9', '(', NOP, 0x80 F,
/* 0b */ '0', ')', NOP, 0x81 F,
/* 0c */ '-', '_', NOP, 0x82 F,
/* 0d */ '=', '+', NOP, 0x83 F,
/* 0e */ 'h'C, 0x0e F, 0x7f, NOP,
/* 0f */ 'i'C, 0x0f F, NOP, NOP,
/* 10 */ 'q'A, 'Q', 'q'C, 0x10 F,
/* 11 */ 'w'A, 'W', 'w'C, 0x11 F,
/* 12 */ 'e'A, 'E', 'e'C, 0x12 F,
/* 13 */ 'r'A, 'R', 'r'C, 0x13 F,
/* 14 */ 't'A, 'T', 't'C, 0x14 F,
/* 15 */ 'y'A, 'Y', 'y'C, 0x15 F,
/* 16 */ 'u'A, 'U', 'u'C, 0x16 F,
/* 17 */ 'i'A, 'I', 'i'C, 0x17 F,
/* 18 */ 'o'A, 'O', 'o'C, 0x18 F,
/* 19 */ 'p'A, 'P', 'p'C, 0x19 F,
/* 1a */ '[', '{', '['C, NOP,
/* 1b */ ']', '}', ']'C, NOP,
/* 1c */ 'm'C, 'm'C, NOP, NOP,
/* 1d */ CTRL, CTRL, CTRL, CTRL,
/* 1e */ 'a'A, 'A', 'a'C, 0x1e F,
/* 1f */ 's'A, 'S', 's'C, 0x1f F,
/* 20 */ 'd'A, 'D', 'd'C, 0x20 F,
/* 21 */ 'f'A, 'F', 'f'C, 0x21 F,
/* 22 */ 'g'A, 'G', 'g'C, 0x22 F,
/* 23 */ 'h'A, 'H', 'h'C, 0x23 F,
/* 24 */ 'j'A, 'J', 'j'C, 0x24 F,
/* 25 */ 'k'A, 'K', 'k'C, 0x25 F,
/* 26 */ 'l'A, 'L', 'l'C, 0x26 F,
/* 27 */ ';', ':', NOP, NOP,
/* 28 */ '\'', '"', NOP, NOP,
/* 29 */ '`', '~', NOP, NOP,
/* 2a */ LSHIFT, LSHIFT, LSHIFT, LSHIFT,
/* 2b */ '\\', '|', '\\'C, NOP,
/* 2c */ 'z'A, 'Z', 'z'C, 0x2c F,
/* 2d */ 'x'A, 'X', 'x'C, 0x2d F,
/* 2e */ 'c'A, 'C', 'c'C, 0x2e F,
/* 2f */ 'v'A, 'V', 'v'C, 0x2f F,
/* 30 */ 'b'A, 'B', 'b'C, 0x30 F,
/* 31 */ 'n'A, 'N', 'n'C, 0x31 F,
/* 32 */ 'm'A, 'M', 'm'C, 0x32 F,
/* 33 */ ',', '<', NOP, NOP,
/* 34 */ '.', '>', NOP, NOP,
/* 35 */ '/', '?', NOP, NOP,
/* 36 */ RSHIFT, RSHIFT, RSHIFT, RSHIFT,
/* 37 */ '*', NOP, NOP, NOP, /* * PrtSc */
/* 38 */ ALT, ALT, ALT, ALT,
/* 39 */ ' ', ' ', NOP, NOP,
/* 3a */ CAPSLK, CAPSLK, CAPSLK, CAPSLK,
/* 3b */ 0x3b F, 0x54 F, 0x5e F, 0x68 F,
/* 3c */ 0x3c F, 0x55 F, 0x5f F, 0x69 F,
/* 3d */ 0x3d F, 0x56 F, 0x60 F, 0x6a F,
/* 3e */ 0x3e F, 0x57 F, 0x61 F, 0x6b F,
/* 3f */ 0x3f F, 0x58 F, 0x62 F, 0x6c F,
/* 40 */ 0x40 F, 0x59 F, 0x63 F, 0x6d F,
/* 41 */ 0x41 F, 0x5a F, 0x64 F, 0x6e F,
/* 42 */ 0x42 F, 0x5b F, 0x65 F, 0x6f F,
/* 43 */ 0x43 F, 0x5c F, 0x66 F, 0x70 F,
/* 44 */ 0x44 F, 0x5d F, 0x67 F, 0x71 F,
/* 45 */ NUMLK, NUMLK, NUMLK, NUMLK,
/* 46 */ SCRLLK, SCRLLK, SCRLLK, SCRLLK,
/* 47 */ 0x47 N, '7', NOP, NOP,
/* 48 */ 0x48 N, '8', NOP, NOP,
/* 49 */ 0x49 N, '9', NOP, NOP,
/* 4a */ '-', '-', NOP, NOP,
/* 4b */ 0x4b N, '4', NOP, NOP,
/* 4c */ NOP, '5', NOP, NOP,
/* 4d */ 0x4d N, '6', NOP, NOP,
/* 4e */ '+', '+', NOP, NOP,
/* 4f */ 0x4f N, '1', NOP, NOP,
/* 50 */ 0x50 N, '2', NOP, NOP,
/* 51 */ 0x51 N, '3', NOP, NOP,
/* 52 */ 0x52 N, '0', NOP, NOP,
/* 53 */ 0x53 N, '.', REBOOT, REBOOT,
/* 54 */ NOP, NOP, NOP, NOP, /* SysReq */
/* 55 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 56 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 57 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 58 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 59 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 5a */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 5b */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 5c */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 5d */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 5e */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 5f */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 60 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 61 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 62 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 63 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 64 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 65 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 66 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 67 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 68 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 69 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 6a */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 6b */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 6c */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 6d */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 6e */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 6f */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 70 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 71 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 72 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 73 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 74 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 75 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 76 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 77 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 78 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 79 */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 7a */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 7b */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 7c */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 7d */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 7e */ UNDEF, UNDEF, UNDEF, UNDEF,
/* 7f */ UNDEF, UNDEF, UNDEF, UNDEF,
};
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#ifndef _BOOT_KEYBOARD_TABLE_H
#define _BOOT_KEYBOARD_TABLE_H
/*
* Structure of the keyboard table for the bootstrap simple
* keyboard driver.
*/
#ifdef __cplusplus
extern "C" {
#endif
#define KBTYPE_NORMAL 0x000 /* Normal keys, process mindlessly. */
#define KBTYPE_ALPHA 0x100 /* Alpha. If CapsLock is set, swap */
/* shifted and unshifted meanings. */
/* Set this on the unshifted character */
#define KBTYPE_NUMPAD 0x200 /* Numeric/Arrow Pad. If NumLock is set, */
/* swap shifted and unshifted meanings. */
/* Set this on the unshifted character. */
#define KBTYPE_FUNC 0x300 /* Extended Function. Send this code, */
/* prefixed with zero. */
#define KBTYPE_SPEC 0x400 /* One-of-a-kind codes. Self-explanatory. */
#define KBTYPE_SPEC_NOP (KBTYPE_SPEC | 0x00)
#define KBTYPE_SPEC_UNDEF (KBTYPE_SPEC | 0x01)
#define KBTYPE_SPEC_LSHIFT (KBTYPE_SPEC | 0x02)
#define KBTYPE_SPEC_RSHIFT (KBTYPE_SPEC | 0x03)
#define KBTYPE_SPEC_CTRL (KBTYPE_SPEC | 0x04)
#define KBTYPE_SPEC_ALT (KBTYPE_SPEC | 0x05)
#define KBTYPE_SPEC_CAPS_LOCK (KBTYPE_SPEC | 0x06)
#define KBTYPE_SPEC_NUM_LOCK (KBTYPE_SPEC | 0x07)
#define KBTYPE_SPEC_SCROLL_LOCK (KBTYPE_SPEC | 0x08)
#define KBTYPE_SPEC_MAYBE_REBOOT (KBTYPE_SPEC | 0x09)
struct keyboard_translate {
unsigned short normal;
unsigned short shifted;
unsigned short ctrled;
unsigned short alted;
};
extern struct keyboard_translate keyboard_translate[128];
#ifdef __cplusplus
}
#endif
#endif /* _BOOT_KEYBOARD_TABLE_H */
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* WARNING: This file is used by both dboot and the kernel.
*/
#include <sys/param.h>
#include <sys/machparam.h>
#include <sys/mach_mmu.h>
#ifdef __xpv
#include <sys/hypervisor.h>
#endif
#ifdef _BOOT
#include <dboot/dboot_printf.h>
#define bop_panic dboot_panic
#else
#include <sys/bootconf.h>
#endif
uint_t shift_amt_nopae[] = {12, 22};
uint_t shift_amt_pae[] = {12, 21, 30, 39};
uint_t *shift_amt;
uint_t ptes_per_table;
uint_t pte_size;
uint32_t lpagesize;
paddr_t top_page_table;
uint_t top_level;
/*
* Return the index corresponding to a virt address at a given page table level.
*/
static uint_t
vatoindex(uint64_t va, uint_t level)
{
return ((va >> shift_amt[level]) & (ptes_per_table - 1));
}
/*
* Return a pointer to the page table entry that maps a virtual address.
* If there is no page table and probe_only is not set, one is created.
*/
x86pte_t *
find_pte(uint64_t va, paddr_t *pa, uint_t level, uint_t probe_only)
{
uint_t l;
uint_t index;
paddr_t table;
if (pa)
*pa = 0;
#ifndef _BOOT
if (IN_HYPERVISOR_VA(va))
return (NULL);
#endif
/*
* Walk down the page tables creating any needed intermediate tables.
*/
table = top_page_table;
for (l = top_level; l != level; --l) {
uint64_t pteval;
paddr_t new_table;
index = vatoindex(va, l);
pteval = get_pteval(table, index);
/*
* Life is easy if we find the pagetable. We just use it.
*/
if (pteval & PT_VALID) {
table = ma_to_pa(pteval & MMU_PAGEMASK);
if (table == -1) {
if (probe_only)
return (NULL);
bop_panic("find_pte(): phys not found!");
}
continue;
}
if (probe_only)
return (NULL);
new_table = make_ptable(&pteval, l);
set_pteval(table, index, l, pteval);
table = new_table;
}
/*
* Return a pointer into the current pagetable.
*/
index = vatoindex(va, l);
if (pa)
*pa = table + index * pte_size;
return (map_pte(table, index));
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#ifndef _BOOT_SERIAL_H
#define _BOOT_SERIAL_H
#ifdef __cplusplus
extern "C" {
#endif
/* ---- ports on 16550 serial chips ---- */
#define DAT 0 /* ... data */
#define ICR 1 /* ... intr control reg */
#define ISR 2 /* ... intr status reg */
#define LCR 3 /* ... line control reg */
#define MCR 4 /* ... modem control reg */
#define LSR 5 /* ... line status reg */
#define MSR 6 /* ... modem status reg */
#define DLL 0 /* ... data latch low (used for baud rate) */
#define DLH 1 /* ... data latch high (ditto) */
#define FIFOR ISR /* ... fifo write reg */
/* ---- LSR bits ---- */
#define RCA 0x01 /* ... receive char avail */
#define XHRE 0x20 /* ... xmit hold buffer empty */
/* ---- Modem bits ---- */
#define DTR 0x01
#define RTS 0x02
#define OUT2 0x08
#define FIFO_ON 0x01
#define FIFO_OFF 0x00
#define FIFORXFLSH 0x02
#define FIFOTXFLSH 0x04
#define FIFODMA 0x08
/* ---- LCR bits ---- */
#define STOP1 00
#define STOP2 0x04
#define BITS5 0x00 /* 5 bits per char */
#define BITS6 0x01 /* 6 bits per char */
#define BITS7 0x02 /* 7 bits per char */
#define BITS8 0x03 /* 8 bits per char */
/* baud rate definitions */
#define DLAB 0x80 /* divisor latch access bit */
#define ASY110 1047 /* 110 baud rate for serial console */
#define ASY150 768 /* 150 baud rate for serial console */
#define ASY300 384 /* 300 baud rate for serial console */
#define ASY600 192 /* 600 baud rate for serial console */
#define ASY1200 96 /* 1200 baud rate for serial console */
#define ASY2400 48 /* 2400 baud rate for serial console */
#define ASY4800 24 /* 4800 baud rate for serial console */
#define ASY9600 12 /* 9600 baud rate for serial console */
#define ASY19200 6 /* 19200 baud rate for serial console */
#define ASY38400 3 /* 38400 baud rate for serial console */
#define ASY57600 2 /* 57600 baud rate for serial console */
#define ASY115200 1 /* 115200 baud rate for serial console */
/*
* Defines for the serial port
*/
#define SERIAL_FIFO_FLUSH 16 /* maximum number of chars to flush */
/* ---- Bit 11 defines direct serial port ---- */
#define SDIRECT 0x1000
/* ---- Bits 9-10 define flow control ---- */
#define SSOFT 0x800
#define SHARD 0x400
/* ---- Bits 5-8 define baud rate ---- */
#define S110 0x00
#define S150 0x20
#define S300 0x40
#define S600 0x60
#define S1200 0x80
#define S2400 0xa0
#define S4800 0xc0
#define S9600 0xe0
#define S19200 0x100
#define S38400 0x120
#define S57600 0x140
#define S76800 0x160
#define S115200 0x180
#define S153600 0x1a0
#define S230400 0x1c0
#define S307200 0x1e0
#define S460800 0x200
/* ---- Bits 3 & 4 are parity ---- */
#define PARITY_NONE 0x10
#define PARITY_ODD 0x08
#define PARITY_EVEN 0x18
/* ---- Bit 2 is stop bit ---- */
#define STOP_1 0x00
#define STOP_2 0x04
/* ---- Bits 0 & 1 are data bits ---- */
#define DATA_8 0x03
#define DATA_7 0x02
#define DATA_6 0x01
#define DATA_5 0x00
/* ---- Line Status ---- */
#define SERIAL_TIMEOUT 0x80
#define SERIAL_XMITSHFT 0x40
#define SERIAL_XMITHOLD 0x20
#define SERIAL_BREAK 0x10
#define SERIAL_FRAME 0x08
#define SERIAL_PARITY 0x04
#define SERIAL_OVERRUN 0x02
#define SERIAL_DATA 0x01
#ifdef __cplusplus
}
#endif
#endif /* _BOOT_SERIAL_H */
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Miniature VGA driver for bootstrap.
*/
#include <sys/archsystm.h>
#include <sys/vgareg.h>
#include <sys/framebuffer.h>
#include <sys/boot_console.h>
#include <sys/rgb.h>
#include "boot_console_impl.h"
#include "boot_console_impl.h"
#if defined(_BOOT)
#include "../dboot/dboot_asm.h"
#include "../dboot/dboot_xboot.h"
#endif
#if defined(__xpv) && defined(_BOOT)
/*
* Device memory address
*
* In dboot under the hypervisor we don't have any memory mappings
* for the first meg of low memory so we can't access devices there.
* Intead we've mapped the device memory that we need to access into
* a local variable within dboot so we can access the device memory
* there.
*/
extern unsigned short *video_fb;
#define VGA_SCREEN (video_fb)
#else /* __xpv && _BOOT */
/* Device memory address */
#define VGA_SCREEN ((uint16_t *)(VGA_MEM_ADDR + VGA_COLOR_BASE))
#endif /* __xpv && _BOOT */
static int cons_color = CONS_COLOR;
static void vga_init(void);
static void vga_drawc(int);
static void vga_setpos(int, int);
static void vga_getpos(int *, int *);
static void vga_scroll(int);
static void vga_clear(int);
static void vga_shiftline(int);
static void vga_eraseline(void);
static void vga_cursor_display(boolean_t);
static void vga_set_crtc(int index, unsigned char val);
static unsigned char vga_get_crtc(int index);
static void vga_set_atr(int index, unsigned char val);
static unsigned char vga_get_atr(int index);
static int
get_vga_color(void)
{
int color;
uint32_t fg, bg;
boot_get_color(&fg, &bg);
color = solaris_color_to_pc_color[bg] << 4;
color |= solaris_color_to_pc_color[fg];
return (color);
}
void
boot_vga_init(bcons_dev_t *bcons_dev)
{
fb_info.terminal.x = VGA_TEXT_COLS;
fb_info.terminal.y = VGA_TEXT_ROWS;
cons_color = get_vga_color();
#if defined(_BOOT)
/*
* Note that we have to enable the cursor before clearing the
* screen since the cursor position is dependant upon the cursor
* skew, which is initialized by vga_cursor_display()
*/
vga_init();
fb_info.cursor.visible = B_FALSE;
vga_cursor_display(B_TRUE);
/*
* In general we should avoid resetting the display during the boot,
* we may have valueable messages there, this why the "native" loader
* boot does pass the console state down to kernel and we do try to
* pick the state. However, the loader is not the only way to boot.
* The non-native boot loaders do not implement the smooth console.
* If we have no information about cursor location, we will get value
* (0, 0) and that means we better clear the screen.
*/
if (fb_info.cursor.pos.x == 0 && fb_info.cursor.pos.y == 0)
vga_clear(cons_color);
vga_setpos(fb_info.cursor.pos.y, fb_info.cursor.pos.x);
#endif /* _BOOT */
bcons_dev->bd_putchar = vga_drawc;
bcons_dev->bd_eraseline = vga_eraseline;
bcons_dev->bd_cursor = vga_cursor_display;
bcons_dev->bd_setpos = vga_setpos;
bcons_dev->bd_shift = vga_shiftline;
}
static void
vga_init(void)
{
unsigned char val;
/* set 16bit colors */
val = vga_get_atr(VGA_ATR_MODE);
val &= ~VGA_ATR_MODE_BLINK;
val &= ~VGA_ATR_MODE_9WIDE;
vga_set_atr(VGA_ATR_MODE, val);
}
static void
vga_cursor_display(boolean_t visible)
{
unsigned char val, msl;
if (fb_info.cursor.visible == visible)
return;
/*
* Figure out the maximum scan line value. We need this to set the
* cursor size.
*/
msl = vga_get_crtc(VGA_CRTC_MAX_S_LN) & 0x1f;
/*
* Enable the cursor and set it's size. Preserve the upper two
* bits of the control register.
* - Bits 0-4 are the starting scan line of the cursor.
* Scanning is done from top-to-bottom. The top-most scan
* line is 0 and the bottom most scan line is the maximum scan
* line value.
* - Bit 5 is the cursor disable bit.
*/
val = vga_get_crtc(VGA_CRTC_CSSL) & 0xc0;
if (visible == B_FALSE)
val |= (1 << 5);
vga_set_crtc(VGA_CRTC_CSSL, val);
/*
* Continue setting the cursors size.
* - Bits 0-4 are the ending scan line of the cursor.
* Scanning is done from top-to-bottom. The top-most scan
* line is 0 and the bottom most scan line is the maximum scan
* line value.
* - Bits 5-6 are the cursor skew.
*/
vga_set_crtc(VGA_CRTC_CESL, msl);
}
static void
vga_eraseline_impl(int x, int y, int color)
{
unsigned short val, *buf;
int i;
buf = VGA_SCREEN + x + y * VGA_TEXT_COLS;
val = (color << 8) | ' ';
for (i = x; i < VGA_TEXT_COLS; i++)
buf[i] = val;
}
static void
vga_eraseline(void)
{
int x, y;
x = fb_info.cursor.pos.x;
y = fb_info.cursor.pos.y;
vga_eraseline_impl(x, y, cons_color);
}
static void
vga_shiftline(int chars)
{
unsigned short *src, *dst;
int x, y, len;
x = fb_info.cursor.pos.x;
y = fb_info.cursor.pos.y;
len = VGA_TEXT_COLS - x - chars;
if (len <= 0)
return;
src = VGA_SCREEN + x + y * VGA_TEXT_COLS;
dst = src + chars;
if (dst <= src) {
do {
*dst++ = *src++;
} while (--len != 0);
} else {
dst += len;
src += len;
do {
*--dst = *--src;
} while (--len != 0);
}
}
static void
vga_clear(int color)
{
int i;
for (i = 0; i < VGA_TEXT_ROWS; i++)
vga_eraseline_impl(0, i, color);
}
static void
vga_drawc(int c)
{
int row;
int col;
vga_getpos(&row, &col);
if (c == '\n') {
if (row < fb_info.terminal.y - 1)
vga_setpos(row + 1, col);
else
vga_scroll(cons_color);
return;
}
/*
* VGA_SCREEN is an array of 16-bit unsigned ints, we do let
* the compiler to take care of truncation here.
*/
VGA_SCREEN[row * VGA_TEXT_COLS + col] = (cons_color << 8) | c;
if (col < VGA_TEXT_COLS - 1)
vga_setpos(row, col + 1);
else if (row < VGA_TEXT_ROWS - 1)
vga_setpos(row + 1, 0);
else {
vga_setpos(row, 0);
vga_scroll(cons_color);
}
}
static void
vga_scroll(int color)
{
int i;
for (i = 0; i < (VGA_TEXT_ROWS - 1) * VGA_TEXT_COLS; i++) {
VGA_SCREEN[i] = VGA_SCREEN[i + VGA_TEXT_COLS];
}
vga_eraseline_impl(0, VGA_TEXT_ROWS - 1, color);
}
static void
vga_setpos(int row, int col)
{
int off;
if (row < 0)
row = 0;
if (row >= fb_info.terminal.y)
row = fb_info.terminal.y - 1;
if (col < 0)
col = 0;
if (col >= fb_info.terminal.x)
col = fb_info.terminal.x - 1;
off = row * VGA_TEXT_COLS + col;
vga_set_crtc(VGA_CRTC_CLAH, off >> 8);
vga_set_crtc(VGA_CRTC_CLAL, off & 0xff);
fb_info.cursor.pos.y = row;
fb_info.cursor.pos.x = col;
}
static void
vga_getpos(int *row, int *col)
{
int off;
off = (vga_get_crtc(VGA_CRTC_CLAH) << 8) + vga_get_crtc(VGA_CRTC_CLAL);
*row = off / VGA_TEXT_COLS;
*col = off % VGA_TEXT_COLS;
}
static void
vga_set_atr(int index, unsigned char val)
{
(void) inb(VGA_REG_ADDR + CGA_STAT);
outb(VGA_REG_ADDR + VGA_ATR_AD, index);
outb(VGA_REG_ADDR + VGA_ATR_AD, val);
(void) inb(VGA_REG_ADDR + CGA_STAT);
outb(VGA_REG_ADDR + VGA_ATR_AD, VGA_ATR_ENB_PLT);
}
static unsigned char
vga_get_atr(int index)
{
unsigned char val;
(void) inb(VGA_REG_ADDR + CGA_STAT);
outb(VGA_REG_ADDR + VGA_ATR_AD, index);
val = inb(VGA_REG_ADDR + VGA_ATR_DATA);
(void) inb(VGA_REG_ADDR + CGA_STAT);
outb(VGA_REG_ADDR + VGA_ATR_AD, VGA_ATR_ENB_PLT);
return (val);
}
static void
vga_set_crtc(int index, unsigned char val)
{
outb(VGA_REG_ADDR + VGA_CRTC_ADR, index);
outb(VGA_REG_ADDR + VGA_CRTC_DATA, val);
}
static unsigned char
vga_get_crtc(int index)
{
outb(VGA_REG_ADDR + VGA_CRTC_ADR, index);
return (inb(VGA_REG_ADDR + VGA_CRTC_DATA));
}
|