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
|
/*
* 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>
*/
/*
* Graphics support for loader emulation.
* The interface in loader and here needs some more development.
* We can get colormap from gfx_private, but loader is currently
* relying on tem fg/bg colors for drawing, once the menu code
* will get some facelift, we would need to provide colors as menu component
* attributes and stop depending on tem.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/fbio.h>
#include <string.h>
#include "gfx_fb.h"
struct framebuffer fb;
#define max(x, y) ((x) >= (y) ? (x) : (y))
static void gfx_fb_cons_display(uint32_t, uint32_t,
uint32_t, uint32_t, uint8_t *);
/* This colormap should be replaced by colormap query from kernel */
typedef struct {
uint8_t red[16];
uint8_t green[16];
uint8_t blue[16];
} text_cmap_t;
text_cmap_t cmap4_to_24 = {
/* BEGIN CSTYLED */
/* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Wh+ Bk Bl Gr Cy Rd Mg Br Wh Bk+ Bl+ Gr+ Cy+ Rd+ Mg+ Yw */
.red = {0xff,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x40,0x00,0x00,0x00,0xff,0xff,0xff},
.green = {0xff,0x00,0x00,0x80,0x80,0x00,0x00,0x80,0x80,0x40,0x00,0xff,0xff,0x00,0x00,0xff},
.blue = {0xff,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x40,0xff,0x00,0xff,0x00,0xff,0x00}
/* END CSTYLED */
};
const uint8_t solaris_color_to_pc_color[16] = {
15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
};
void
gfx_framework_init(void)
{
struct fbgattr attr;
struct gfxfb_info *gfxfb_info;
char buf[10];
fb.fd = open("/dev/fb", O_RDWR);
if (fb.fd < 0)
return;
/* make sure we have GFX framebuffer */
if (ioctl(fb.fd, VIS_GETIDENTIFIER, &fb.ident) < 0 ||
strcmp(fb.ident.name, "illumos_fb") != 0) {
(void) close(fb.fd);
fb.fd = -1;
return;
}
if (ioctl(fb.fd, FBIOGATTR, &attr) < 0) {
(void) close(fb.fd);
fb.fd = -1;
return;
}
gfxfb_info = (struct gfxfb_info *)attr.sattr.dev_specific;
fb.fb_height = attr.fbtype.fb_height;
fb.fb_width = attr.fbtype.fb_width;
fb.fb_depth = attr.fbtype.fb_depth;
fb.fb_size = attr.fbtype.fb_size;
fb.fb_bpp = attr.fbtype.fb_depth >> 3;
if (attr.fbtype.fb_depth == 15)
fb.fb_bpp = 2;
fb.fb_pitch = gfxfb_info->pitch;
fb.terminal_origin_x = gfxfb_info->terminal_origin_x;
fb.terminal_origin_y = gfxfb_info->terminal_origin_y;
fb.font_width = gfxfb_info->font_width;
fb.font_height = gfxfb_info->font_height;
fb.red_mask_size = gfxfb_info->red_mask_size;
fb.red_field_position = gfxfb_info->red_field_position;
fb.green_mask_size = gfxfb_info->green_mask_size;
fb.green_field_position = gfxfb_info->green_field_position;
fb.blue_mask_size = gfxfb_info->blue_mask_size;
fb.blue_field_position = gfxfb_info->blue_field_position;
fb.fb_addr = (uint8_t *)mmap(0, fb.fb_size, (PROT_READ | PROT_WRITE),
MAP_SHARED, fb.fd, 0);
if (fb.fb_addr == NULL) {
(void) close(fb.fd);
fb.fd = -1;
return;
}
(void) snprintf(buf, sizeof (buf), "%d", fb.fb_height);
(void) setenv("screen-height", buf, 1);
(void) snprintf(buf, sizeof (buf), "%d", fb.fb_width);
(void) setenv("screen-width", buf, 1);
}
void
gfx_framework_fini(void)
{
if (fb.fd < 0)
return;
(void) munmap((caddr_t)fb.fb_addr, fb.fb_size);
(void) close(fb.fd);
fb.fd = -1;
}
static int
isqrt(int num)
{
int res = 0;
int bit = 1 << 30;
/* "bit" starts at the highest power of four <= the argument. */
while (bit > num)
bit >>= 2;
while (bit != 0) {
if (num >= res + bit) {
num -= res + bit;
res = (res >> 1) + bit;
} else {
res >>= 1;
}
bit >>= 2;
}
return (res);
}
void
gfx_fb_setpixel(uint32_t x, uint32_t y)
{
uint32_t c, offset;
if (fb.fd < 0)
return;
c = 0; /* black */
if (x >= fb.fb_width || y >= fb.fb_height)
return;
offset = y * fb.fb_pitch + x * fb.fb_bpp;
switch (fb.fb_depth) {
case 8:
fb.fb_addr[offset] = c & 0xff;
break;
case 15:
case 16:
*(uint16_t *)(fb.fb_addr + offset) = c & 0xffff;
break;
case 24:
fb.fb_addr[offset] = (c >> 16) & 0xff;
fb.fb_addr[offset + 1] = (c >> 8) & 0xff;
fb.fb_addr[offset + 2] = c & 0xff;
break;
case 32:
*(uint32_t *)(fb.fb_addr + offset) = c;
break;
}
}
void
gfx_fb_drawrect(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2,
uint32_t fill)
{
int x, y;
if (fb.fd < 0)
return;
for (y = y1; y <= y2; y++) {
if (fill || (y == y1) || (y == y2)) {
for (x = x1; x <= x2; x++)
gfx_fb_setpixel(x, y);
} else {
gfx_fb_setpixel(x1, y);
gfx_fb_setpixel(x2, y);
}
}
}
void
gfx_term_drawrect(uint32_t row1, uint32_t col1, uint32_t row2, uint32_t col2)
{
int x1, y1, x2, y2;
int xshift, yshift;
int width, i;
if (fb.fd < 0)
return;
width = fb.font_width / 4; /* line width */
xshift = (fb.font_width - width) / 2;
yshift = (fb.font_height - width) / 2;
/* Terminal coordinates start from (1,1) */
row1--;
col1--;
row2--;
col2--;
/*
* Draw horizontal lines width points thick, shifted from outer edge.
*/
x1 = (row1 + 1) * fb.font_width + fb.terminal_origin_x;
y1 = col1 * fb.font_height + fb.terminal_origin_y + yshift;
x2 = row2 * fb.font_width + fb.terminal_origin_x;
gfx_fb_drawrect(x1, y1, x2, y1 + width, 1);
y2 = col2 * fb.font_height + fb.terminal_origin_y;
y2 += fb.font_height - yshift - width;
gfx_fb_drawrect(x1, y2, x2, y2 + width, 1);
/*
* Draw vertical lines width points thick, shifted from outer edge.
*/
x1 = row1 * fb.font_width + fb.terminal_origin_x + xshift;
y1 = col1 * fb.font_height + fb.terminal_origin_y;
y1 += fb.font_height;
y2 = col2 * fb.font_height + fb.terminal_origin_y;
gfx_fb_drawrect(x1, y1, x1 + width, y2, 1);
x1 = row2 * fb.font_width + fb.terminal_origin_x;
x1 += fb.font_width - xshift - width;
gfx_fb_drawrect(x1, y1, x1 + width, y2, 1);
/* Draw upper left corner. */
x1 = row1 * fb.font_width + fb.terminal_origin_x + xshift;
y1 = col1 * fb.font_height + fb.terminal_origin_y;
y1 += fb.font_height;
x2 = row1 * fb.font_width + fb.terminal_origin_x;
x2 += fb.font_width;
y2 = col1 * fb.font_height + fb.terminal_origin_y + yshift;
for (i = 0; i <= width; i++)
gfx_fb_bezier(x1 + i, y1, x1 + i, y2 + i, x2, y2 + i, width-i);
/* Draw lower left corner. */
x1 = row1 * fb.font_width + fb.terminal_origin_x;
x1 += fb.font_width;
y1 = col2 * fb.font_height + fb.terminal_origin_y;
y1 += fb.font_height - yshift;
x2 = row1 * fb.font_width + fb.terminal_origin_x + xshift;
y2 = col2 * fb.font_height + fb.terminal_origin_y;
for (i = 0; i <= width; i++)
gfx_fb_bezier(x1, y1 - i, x2 + i, y1 - i, x2 + i, y2, width-i);
/* Draw upper right corner. */
x1 = row2 * fb.font_width + fb.terminal_origin_x;
y1 = col1 * fb.font_height + fb.terminal_origin_y + yshift;
x2 = row2 * fb.font_width + fb.terminal_origin_x;
x2 += fb.font_width - xshift - width;
y2 = col1 * fb.font_height + fb.terminal_origin_y;
y2 += fb.font_height;
for (i = 0; i <= width; i++)
gfx_fb_bezier(x1, y1 + i, x2 + i, y1 + i, x2 + i, y2, width-i);
/* Draw lower right corner. */
x1 = row2 * fb.font_width + fb.terminal_origin_x;
y1 = col2 * fb.font_height + fb.terminal_origin_y;
y1 += fb.font_height - yshift;
x2 = row2 * fb.font_width + fb.terminal_origin_x;
x2 += fb.font_width - xshift - width;
y2 = col2 * fb.font_height + fb.terminal_origin_y;
for (i = 0; i <= width; i++)
gfx_fb_bezier(x1, y1 - i, x2 + i, y1 - i, x2 + i, y2, width-i);
}
void
gfx_fb_line(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint32_t width)
{
int dx, sx, dy, sy;
int err, e2, x2, y2, ed;
if (fb.fd < 0)
return;
sx = x0 < x1? 1 : -1;
sy = y0 < y1? 1 : -1;
dx = abs(x1 - x0);
dy = abs(y1 - y0);
err = dx - dy;
ed = dx + dy == 0 ? 1 : isqrt(dx * dx + dy * dy);
if (dx != 0 && dy != 0)
width = (width + 1) >> 1;
for (;;) {
gfx_fb_setpixel(x0, y0);
e2 = err;
x2 = x0;
if ((e2 << 1) >= -dx) { /* x step */
e2 += dy;
y2 = y0;
while (e2 < ed * width && (y1 != y2 || dx > dy)) {
y2 += sy;
gfx_fb_setpixel(x0, y2);
e2 += dx;
}
if (x0 == x1)
break;
e2 = err;
err -= dy;
x0 += sx;
}
if ((e2 << 1) <= dy) { /* y step */
e2 = dx-e2;
while (e2 < ed * width && (x1 != x2 || dx < dy)) {
x2 += sx;
gfx_fb_setpixel(x2, y0);
e2 += dy;
}
if (y0 == y1)
break;
err += dx;
y0 += sy;
}
}
}
void
gfx_fb_bezier(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint32_t x2,
uint32_t y2, uint32_t wd)
{
int sx, sy, xx, yy, xy, width;
int dx, dy, err, curvature;
int i;
if (fb.fd < 0)
return;
width = wd;
sx = x2 - x1;
sy = y2 - y1;
xx = x0 - x1;
yy = y0 - y1;
curvature = xx*sy - yy*sx;
if (sx * sx + sy * sy > xx * xx + yy * yy) {
x2 = x0;
x0 = sx + x1;
y2 = y0;
y0 = sy + y1;
curvature = -curvature;
}
if (curvature != 0) {
xx += sx;
sx = x0 < x2? 1 : -1;
xx *= sx;
yy += sy;
sy = y0 < y2? 1 : -1;
yy *= sy;
xy = 2 * xx * yy;
xx *= xx;
yy *= yy;
if (curvature * sx * sy < 0) {
xx = -xx;
yy = -yy;
xy = -xy;
curvature = -curvature;
}
dx = 4 * sy * curvature * (x1 - x0) + xx - xy;
dy = 4 * sx * curvature * (y0 - y1) + yy - xy;
xx += xx;
yy += yy;
err = dx + dy + xy;
do {
for (i = 0; i <= width; i++)
gfx_fb_setpixel(x0 + i, y0);
if (x0 == x2 && y0 == y2)
return; /* last pixel -> curve finished */
y1 = 2 * err < dx;
if (2 * err > dy) {
x0 += sx;
dx -= xy;
dy += yy;
err += dy;
}
if (y1 != 0) {
y0 += sy;
dy -= xy;
dx += xx;
err += dx;
}
} while (dy < dx); /* gradient negates -> algorithm fails */
}
gfx_fb_line(x0, y0, x2, y2, width);
}
#define FL_PUTIMAGE_BORDER 0x1
#define FL_PUTIMAGE_NOSCROLL 0x2
#define FL_PUTIMAGE_DEBUG 0x80
int
gfx_fb_putimage(png_t *png, uint32_t ux1, uint32_t uy1, uint32_t ux2,
uint32_t uy2, uint32_t flags)
{
uint32_t i, j, x, y, fheight, fwidth, color;
uint8_t r, g, b, a, *p, *data;
bool scale = false;
bool trace = false;
trace = (flags & FL_PUTIMAGE_DEBUG) != 0;
if (fb.fd < 0) {
if (trace)
printf("Framebuffer not active.\n");
return (1);
}
if (png->color_type != PNG_TRUECOLOR_ALPHA) {
if (trace)
printf("Not truecolor image.\n");
return (1);
}
if (ux1 > fb.fb_width || uy1 > fb.fb_height) {
if (trace)
printf("Top left coordinate off screen.\n");
return (1);
}
if (png->width > UINT16_MAX || png->height > UINT16_MAX) {
if (trace)
printf("Image too large.\n");
return (1);
}
if (png->width < 1 || png->height < 1) {
if (trace)
printf("Image too small.\n");
return (1);
}
/*
* If 0 was passed for either ux2 or uy2, then calculate the missing
* part of the bottom right coordinate.
*/
scale = true;
if (ux2 == 0 && uy2 == 0) {
/* Both 0, use the native resolution of the image */
ux2 = ux1 + png->width;
uy2 = uy1 + png->height;
scale = false;
} else if (ux2 == 0) {
/* Set ux2 from uy2/uy1 to maintain aspect ratio */
ux2 = ux1 + (png->width * (uy2 - uy1)) / png->height;
} else if (uy2 == 0) {
/* Set uy2 from ux2/ux1 to maintain aspect ratio */
uy2 = uy1 + (png->height * (ux2 - ux1)) / png->width;
}
if (ux2 > fb.fb_width || uy2 > fb.fb_height) {
if (trace)
printf("Bottom right coordinate off screen.\n");
return (1);
}
fwidth = ux2 - ux1;
fheight = uy2 - uy1;
/*
* If the original image dimensions have been passed explicitly,
* disable scaling.
*/
if (fwidth == png->width && fheight == png->height)
scale = false;
if (ux1 == 0) {
/*
* No top left X co-ordinate (real coordinates start at 1),
* place as far right as it will fit.
*/
ux2 = fb.fb_width - fb.terminal_origin_x;
ux1 = ux2 - fwidth;
}
if (uy1 == 0) {
/*
* No top left Y co-ordinate (real coordinates start at 1),
* place as far down as it will fit.
*/
uy2 = fb.fb_height - fb.terminal_origin_y;
uy1 = uy2 - fheight;
}
if (ux1 >= ux2 || uy1 >= uy2) {
if (trace)
printf("Image dimensions reversed.\n");
return (1);
}
if (fwidth < 2 || fheight < 2) {
if (trace)
printf("Target area too small\n");
return (1);
}
if (trace)
printf("Image %ux%u -> %ux%u @%ux%u\n",
png->width, png->height, fwidth, fheight, ux1, uy1);
if ((flags & FL_PUTIMAGE_BORDER))
gfx_fb_drawrect(ux1, uy1, ux2, uy2, 0);
data = malloc(fwidth * fheight * fb.fb_bpp);
if (data == NULL) {
if (trace)
printf("Out of memory.\n");
return (1);
}
/*
* Build image for our framebuffer.
*/
/* Helper to calculate the pixel index from the source png */
#define GETPIXEL(xx, yy) (((yy) * png->width + (xx)) * png->bpp)
/*
* For each of the x and y directions, calculate the number of pixels
* in the source image that correspond to a single pixel in the target.
* Use fixed-point arithmetic with 16-bits for each of the integer and
* fractional parts.
*/
const uint32_t wcstep = ((png->width - 1) << 16) / (fwidth - 1);
const uint32_t hcstep = ((png->height - 1) << 16) / (fheight - 1);
uint32_t hc = 0;
for (y = 0; y < fheight; y++) {
uint32_t hc2 = (hc >> 9) & 0x7f;
uint32_t hc1 = 0x80 - hc2;
uint32_t offset_y = hc >> 16;
uint32_t offset_y1 = offset_y + 1;
uint32_t wc = 0;
for (x = 0; x < fwidth; x++) {
uint32_t wc2 = (wc >> 9) & 0x7f;
uint32_t wc1 = 0x80 - wc2;
uint32_t offset_x = wc >> 16;
uint32_t offset_x1 = offset_x + 1;
/* Target pixel index */
j = (y * fwidth + x) * fb.fb_bpp;
if (!scale) {
i = GETPIXEL(x, y);
r = png->image[i];
g = png->image[i + 1];
b = png->image[i + 2];
a = png->image[i + 3];
} else {
uint8_t pixel[4];
uint32_t p00 = GETPIXEL(offset_x, offset_y);
uint32_t p01 = GETPIXEL(offset_x, offset_y1);
uint32_t p10 = GETPIXEL(offset_x1, offset_y);
uint32_t p11 = GETPIXEL(offset_x1, offset_y1);
/*
* Given a 2x2 array of pixels in the source
* image, combine them to produce a single
* value for the pixel in the target image.
* Each column of pixels is combined using
* a weighted average where the top and bottom
* pixels contribute hc1 and hc2 respectively.
* The calculation for bottom pixel pB and
* top pixel pT is:
* (pT * hc1 + pB * hc2) / (hc1 + hc2)
* Once the values are determined for the two
* columns of pixels, then the columns are
* averaged together in the same way but using
* wc1 and wc2 for the weightings.
*
* Since hc1 and hc2 are chosen so that
* hc1 + hc2 == 128 (and same for wc1 + wc2),
* the >> 14 below is a quick way to divide by
* (hc1 + hc2) * (wc1 + wc2)
*/
for (i = 0; i < 4; i++)
pixel[i] = (
(png->image[p00 + i] * hc1 +
png->image[p01 + i] * hc2) * wc1 +
(png->image[p10 + i] * hc1 +
png->image[p11 + i] * hc2) * wc2)
>> 14;
r = pixel[0];
g = pixel[1];
b = pixel[2];
a = pixel[3];
}
color =
r >> (8 - fb.red_mask_size)
<< fb.red_field_position |
g >> (8 - fb.green_mask_size)
<< fb.green_field_position |
b >> (8 - fb.blue_mask_size)
<< fb.blue_field_position;
switch (fb.fb_depth) {
case 8: {
uint32_t best, dist, k;
int diff;
color = 0;
best = 256 * 256 * 256;
for (k = 0; k < 16; k++) {
diff = r - cmap4_to_24.red[k];
dist = diff * diff;
diff = g - cmap4_to_24.green[k];
dist += diff * diff;
diff = b - cmap4_to_24.blue[k];
dist += diff * diff;
if (dist < best) {
color = k;
best = dist;
if (dist == 0)
break;
}
}
data[j] = solaris_color_to_pc_color[color];
break;
}
case 15:
case 16:
*(uint16_t *)(data+j) = color;
break;
case 24:
p = (uint8_t *)&color;
data[j] = p[0];
data[j+1] = p[1];
data[j+2] = p[2];
break;
case 32:
color |= a << 24;
*(uint32_t *)(data+j) = color;
break;
}
wc += wcstep;
}
hc += hcstep;
}
gfx_fb_cons_display(uy1, ux1, fwidth, fheight, data);
free(data);
return (0);
}
/*
* Implements alpha blending for RGBA data, could use pixels for arguments,
* but byte stream seems more generic.
* The generic alpha blending is:
* blend = alpha * fg + (1.0 - alpha) * bg.
* Since our alpha is not from range [0..1], we scale appropriately.
*/
static uint8_t
alpha_blend(uint8_t fg, uint8_t bg, uint8_t alpha)
{
uint16_t blend, h, l;
/* trivial corner cases */
if (alpha == 0)
return (bg);
if (alpha == 0xFF)
return (fg);
blend = (alpha * fg + (0xFF - alpha) * bg);
/* Division by 0xFF */
h = blend >> 8;
l = blend & 0xFF;
if (h + l >= 0xFF)
h++;
return (h);
}
/* Copy memory to framebuffer or to memory. */
static void
bitmap_cpy(uint8_t *dst, uint8_t *src, uint32_t len, int bpp)
{
uint32_t i;
uint8_t a;
switch (bpp) {
case 4:
for (i = 0; i < len; i += bpp) {
a = src[i+3];
dst[i] = alpha_blend(src[i], dst[i], a);
dst[i+1] = alpha_blend(src[i+1], dst[i+1], a);
dst[i+2] = alpha_blend(src[i+2], dst[i+2], a);
dst[i+3] = a;
}
break;
default:
(void) memcpy(dst, src, len);
break;
}
}
/*
* gfx_fb_cons_display implements direct draw on frame buffer memory.
* It is needed till we have way to send bitmaps to tem, tem already has
* function to send data down to framebuffer.
*/
static void
gfx_fb_cons_display(uint32_t row, uint32_t col,
uint32_t width, uint32_t height, uint8_t *data)
{
uint32_t size; /* write size per scanline */
uint8_t *fbp; /* fb + calculated offset */
int i;
/* make sure we will not write past FB */
if (col >= fb.fb_width || row >= fb.fb_height ||
col + width > fb.fb_width || row + height > fb.fb_height)
return;
size = width * fb.fb_bpp;
fbp = fb.fb_addr + col * fb.fb_bpp + row * fb.fb_pitch;
/* write all scanlines in rectangle */
for (i = 0; i < height; i++) {
uint8_t *dest = fbp + i * fb.fb_pitch;
uint8_t *src = data + i * size;
bitmap_cpy(dest, src, size, fb.fb_bpp);
}
}
/*
* 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 Some <tsoome@me.com>
* Copyright 2020 RackTop Systems, Inc.
*/
#ifndef _GFX_FB_H
#define _GFX_FB_H
/*
* Graphics support for loader emulation.
*/
#include <sys/visual_io.h>
#include <pnglite.h>
#ifdef __cplusplus
extern "C" {
#endif
struct framebuffer {
struct vis_identifier ident;
int fd; /* frame buffer device descriptor */
uint8_t *fb_addr; /* mapped framebuffer */
int fb_height; /* in pixels */
int fb_width; /* in pixels */
int fb_depth; /* bits per pixel */
int fb_bpp; /* bytes per pixel */
int fb_size; /* total size in bytes */
int fb_pitch; /* bytes per scanline */
uint16_t terminal_origin_x;
uint16_t terminal_origin_y;
uint16_t font_width;
uint16_t font_height;
uint8_t red_mask_size;
uint8_t red_field_position;
uint8_t green_mask_size;
uint8_t green_field_position;
uint8_t blue_mask_size;
uint8_t blue_field_position;
};
extern struct framebuffer fb;
void gfx_framework_init(void);
void gfx_framework_fini(void);
void gfx_fb_setpixel(uint32_t, uint32_t);
void gfx_fb_drawrect(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
void gfx_term_drawrect(uint32_t, uint32_t, uint32_t, uint32_t);
void gfx_fb_line(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
void gfx_fb_bezier(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t,
uint32_t);
#define FL_PUTIMAGE_BORDER 0x1
#define FL_PUTIMAGE_NOSCROLL 0x2
#define FL_PUTIMAGE_DEBUG 0x80
int gfx_fb_putimage(png_t *, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
#ifdef __cplusplus
}
#endif
#endif /* _GFX_FB_H */
/*
* Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
* Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <limits.h>
#include <unistd.h>
#include <dirent.h>
#include <macros.h>
#include <sys/systeminfo.h>
#include <sys/linker_set.h>
#include <sys/queue.h>
#include <sys/mnttab.h>
#include "loader_emu.h"
#include "gfx_fb.h"
#include "ficl.h"
#define MDIR_REMOVED 0x0001
#define MDIR_NOHINTS 0x0002
struct moduledir {
char *d_path; /* path of modules directory */
uchar_t *d_hints; /* content of linker.hints file */
int d_hintsz; /* size of hints data */
int d_flags;
STAILQ_ENTRY(moduledir) d_link;
};
static STAILQ_HEAD(, moduledir) moduledir_list =
STAILQ_HEAD_INITIALIZER(moduledir_list);
static const char *default_searchpath = "/kernel";
static char typestr[] = "?fc?d?b? ?l?s?w";
static int ls_getdir(char **pathp);
extern char **_environ;
char *command_errmsg;
char command_errbuf[256];
extern void pager_open(void);
extern void pager_close(void);
extern int pager_output(const char *);
extern int pager_file(const char *);
static int page_file(char *);
static int include(const char *);
static int command_help(int argc, char *argv[]);
static int command_commandlist(int argc, char *argv[]);
static int command_show(int argc, char *argv[]);
static int command_set(int argc, char *argv[]);
static int command_setprop(int argc, char *argv[]);
static int command_unset(int argc, char *argv[]);
static int command_echo(int argc, char *argv[]);
static int command_read(int argc, char *argv[]);
static int command_more(int argc, char *argv[]);
static int command_ls(int argc, char *argv[]);
static int command_include(int argc, char *argv[]);
static int command_autoboot(int argc, char *argv[]);
static int command_boot(int argc, char *argv[]);
static int command_unload(int argc, char *argv[]);
static int command_load(int argc, char *argv[]);
static int command_reboot(int argc, char *argv[]);
static int command_sifting(int argc, char *argv[]);
static int command_framebuffer(int argc, char *argv[]);
#define BF_PARSE 100
#define BF_DICTSIZE 30000
/* update when loader version will change */
static const char bootprog_rev[] = "1.1";
/*
* BootForth Interface to Ficl Forth interpreter.
*/
ficlSystem *bf_sys;
ficlVm *bf_vm;
/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Jordan K. Hubbard
* 29 August 1998
*
* The meat of the simple parser.
*/
static void clean(void);
static int insert(int *argcp, char *buf);
#define PARSE_BUFSIZE 1024 /* maximum size of one element */
#define MAXARGS 20 /* maximum number of elements */
static char *args[MAXARGS];
#define DIGIT(x) \
(isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
/*
* backslash: Return malloc'd copy of str with all standard "backslash
* processing" done on it. Original can be free'd if desired.
*/
char *
backslash(char *str)
{
/*
* Remove backslashes from the strings. Turn \040 etc. into a single
* character (we allow eight bit values). Currently NUL is not
* allowed.
*
* Turn "\n" and "\t" into '\n' and '\t' characters. Etc.
*/
char *new_str;
int seenbs = 0;
int i = 0;
if ((new_str = strdup(str)) == NULL)
return (NULL);
while (*str) {
if (seenbs) {
seenbs = 0;
switch (*str) {
case '\\':
new_str[i++] = '\\';
str++;
break;
/* preserve backslashed quotes, dollar signs */
case '\'':
case '"':
case '$':
new_str[i++] = '\\';
new_str[i++] = *str++;
break;
case 'b':
new_str[i++] = '\b';
str++;
break;
case 'f':
new_str[i++] = '\f';
str++;
break;
case 'r':
new_str[i++] = '\r';
str++;
break;
case 'n':
new_str[i++] = '\n';
str++;
break;
case 's':
new_str[i++] = ' ';
str++;
break;
case 't':
new_str[i++] = '\t';
str++;
break;
case 'v':
new_str[i++] = '\13';
str++;
break;
case 'z':
str++;
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': {
char val;
/* Three digit octal constant? */
if (*str >= '0' && *str <= '3' &&
*(str + 1) >= '0' && *(str + 1) <= '7' &&
*(str + 2) >= '0' && *(str + 2) <= '7') {
val = (DIGIT(*str) << 6) +
(DIGIT(*(str + 1)) << 3) +
DIGIT(*(str + 2));
/*
* Allow null value if user really
* wants to shoot at feet, but beware!
*/
new_str[i++] = val;
str += 3;
break;
}
/*
* One or two digit hex constant?
* If two are there they will both be taken.
* Use \z to split them up if this is not
* wanted.
*/
if (*str == '0' &&
(*(str + 1) == 'x' || *(str + 1) == 'X') &&
isxdigit(*(str + 2))) {
val = DIGIT(*(str + 2));
if (isxdigit(*(str + 3))) {
val = (val << 4) +
DIGIT(*(str + 3));
str += 4;
} else
str += 3;
/* Yep, allow null value here too */
new_str[i++] = val;
break;
}
}
break;
default:
new_str[i++] = *str++;
break;
}
} else {
if (*str == '\\') {
seenbs = 1;
str++;
} else
new_str[i++] = *str++;
}
}
if (seenbs) {
/*
* The final character was a '\'.
* Put it in as a single backslash.
*/
new_str[i++] = '\\';
}
new_str[i] = '\0';
return (new_str);
}
/*
* parse: accept a string of input and "parse" it for backslash
* substitutions and environment variable expansions (${var}),
* returning an argc/argv style vector of whitespace separated
* arguments. Returns 0 on success, 1 on failure (ok, ok, so I
* wimped-out on the error codes! :).
*
* Note that the argv array returned must be freed by the caller, but
* we own the space allocated for arguments and will free that on next
* invocation. This allows argv consumers to modify the array if
* required.
*
* NB: environment variables that expand to more than one whitespace
* separated token will be returned as a single argv[] element, not
* split in turn. Expanded text is also immune to further backslash
* elimination or expansion since this is a one-pass, non-recursive
* parser. You didn't specify more than this so if you want more, ask
* me. - jkh
*/
#define PARSE_FAIL(expr) \
if (expr) { \
printf("fail at line %d\n", __LINE__); \
clean(); \
free(copy); \
free(buf); \
return (1); \
}
/* Accept the usual delimiters for a variable, returning counterpart */
static char
isdelim(int ch)
{
if (ch == '{')
return ('}');
else if (ch == '(')
return (')');
return ('\0');
}
static int
isquote(int ch)
{
return (ch == '\'');
}
static int
isdquote(int ch)
{
return (ch == '"');
}
int
parse(int *argc, char ***argv, char *str)
{
int ac;
char *val, *p, *q, *copy = NULL;
size_t i = 0;
char token, tmp, quote, dquote, *buf;
enum { STR, VAR, WHITE } state;
ac = *argc = 0;
dquote = quote = 0;
if (!str || (p = copy = backslash(str)) == NULL)
return (1);
/* Initialize vector and state */
clean();
state = STR;
buf = (char *)malloc(PARSE_BUFSIZE);
token = 0;
/* And awaaaaaaaaay we go! */
while (*p) {
switch (state) {
case STR:
if ((*p == '\\') && p[1]) {
p++;
PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
buf[i++] = *p++;
} else if (isquote(*p)) {
quote = quote ? 0 : *p;
if (dquote) { /* keep quote */
PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
buf[i++] = *p++;
} else
++p;
} else if (isdquote(*p)) {
dquote = dquote ? 0 : *p;
if (quote) { /* keep dquote */
PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
buf[i++] = *p++;
} else
++p;
} else if (isspace(*p) && !quote && !dquote) {
state = WHITE;
if (i) {
buf[i] = '\0';
PARSE_FAIL(insert(&ac, buf));
i = 0;
}
++p;
} else if (*p == '$' && !quote) {
token = isdelim(*(p + 1));
if (token)
p += 2;
else
++p;
state = VAR;
} else {
PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
buf[i++] = *p++;
}
break;
case WHITE:
if (isspace(*p))
++p;
else
state = STR;
break;
case VAR:
if (token) {
PARSE_FAIL((q = strchr(p, token)) == NULL);
} else {
q = p;
while (*q && !isspace(*q))
++q;
}
tmp = *q;
*q = '\0';
if ((val = getenv(p)) != NULL) {
size_t len = strlen(val);
(void) strncpy(buf + i, val,
PARSE_BUFSIZE - (i + 1));
i += min(len, PARSE_BUFSIZE - 1);
}
*q = tmp; /* restore value */
p = q + (token ? 1 : 0);
state = STR;
break;
}
}
/* missing terminating ' or " */
PARSE_FAIL(quote || dquote);
/* If at end of token, add it */
if (i && state == STR) {
buf[i] = '\0';
PARSE_FAIL(insert(&ac, buf));
}
args[ac] = NULL;
*argc = ac;
*argv = (char **)malloc((sizeof (char *) * ac + 1));
bcopy(args, *argv, sizeof (char *) * ac + 1);
free(buf);
free(copy);
return (0);
}
#define MAXARGS 20
/* Clean vector space */
static void
clean(void)
{
int i;
for (i = 0; i < MAXARGS; i++) {
if (args[i] != NULL) {
free(args[i]);
args[i] = NULL;
}
}
}
static int
insert(int *argcp, char *buf)
{
if (*argcp >= MAXARGS)
return (1);
args[(*argcp)++] = strdup(buf);
return (0);
}
static char *
isadir(void)
{
char *buf;
size_t bufsize = 20;
int ret;
if ((buf = malloc(bufsize)) == NULL)
return (NULL);
ret = sysinfo(SI_ARCHITECTURE_K, buf, bufsize);
if (ret == -1) {
free(buf);
return (NULL);
}
return (buf);
}
/*
* Shim for taking commands from BF and passing them out to 'standard'
* argv/argc command functions.
*/
static void
bf_command(ficlVm *vm)
{
char *name, *line, *tail, *cp;
size_t len;
struct bootblk_command **cmdp;
bootblk_cmd_t *cmd;
int nstrings, i;
int argc, result;
char **argv;
/* Get the name of the current word */
name = vm->runningWord->name;
/* Find our command structure */
cmd = NULL;
SET_FOREACH(cmdp, Xcommand_set) {
if (((*cmdp)->c_name != NULL) &&
strcmp(name, (*cmdp)->c_name) == 0)
cmd = (*cmdp)->c_fn;
}
if (cmd == NULL)
printf("callout for unknown command '%s'\n", name);
/* Check whether we have been compiled or are being interpreted */
if (ficlStackPopInteger(ficlVmGetDataStack(vm))) {
/*
* Get parameters from stack, in the format:
* an un ... a2 u2 a1 u1 n --
* Where n is the number of strings, a/u are pairs of
* address/size for strings, and they will be concatenated
* in LIFO order.
*/
nstrings = ficlStackPopInteger(ficlVmGetDataStack(vm));
for (i = 0, len = 0; i < nstrings; i++)
len += ficlStackFetch(ficlVmGetDataStack(vm),
i * 2).i + 1;
line = malloc(strlen(name) + len + 1);
(void) strcpy(line, name);
if (nstrings)
for (i = 0; i < nstrings; i++) {
len = ficlStackPopInteger(
ficlVmGetDataStack(vm));
cp = ficlStackPopPointer(
ficlVmGetDataStack(vm));
(void) strcat(line, " ");
(void) strncat(line, cp, len);
}
} else {
/* Get remainder of invocation */
tail = ficlVmGetInBuf(vm);
for (cp = tail, len = 0;
cp != vm->tib.end && *cp != 0 && *cp != '\n'; cp++, len++)
;
line = malloc(strlen(name) + len + 2);
(void) strcpy(line, name);
if (len > 0) {
(void) strcat(line, " ");
(void) strncat(line, tail, len);
ficlVmUpdateTib(vm, tail + len);
}
}
command_errmsg = command_errbuf;
command_errbuf[0] = 0;
if (!parse(&argc, &argv, line)) {
result = (cmd)(argc, argv);
free(argv);
} else {
result = BF_PARSE;
}
free(line);
/*
* If there was error during nested ficlExec(), we may no longer have
* valid environment to return. Throw all exceptions from here.
*/
if (result != 0)
ficlVmThrow(vm, result);
/* This is going to be thrown!!! */
ficlStackPushInteger(ficlVmGetDataStack(vm), result);
}
static char *
get_currdev(void)
{
int ret;
char *currdev;
FILE *fp;
struct mnttab mpref = {0};
struct mnttab mp = {0};
mpref.mnt_mountp = "/";
fp = fopen(MNTTAB, "r");
/* do the best we can to return something... */
if (fp == NULL)
return (strdup(":"));
ret = getmntany(fp, &mp, &mpref);
(void) fclose(fp);
if (ret == 0)
(void) asprintf(&currdev, "zfs:%s:", mp.mnt_special);
else
return (strdup(":"));
return (currdev);
}
/*
* Replace a word definition (a builtin command) with another
* one that:
*
* - Throw error results instead of returning them on the stack
* - Pass a flag indicating whether the word was compiled or is
* being interpreted.
*
* There is one major problem with builtins that cannot be overcome
* in anyway, except by outlawing it. We want builtins to behave
* differently depending on whether they have been compiled or they
* are being interpreted. Notice that this is *not* the interpreter's
* current state. For example:
*
* : example ls ; immediate
* : problem example ; \ "ls" gets executed while compiling
* example \ "ls" gets executed while interpreting
*
* Notice that, though the current state is different in the two
* invocations of "example", in both cases "ls" has been
* *compiled in*, which is what we really want.
*
* The problem arises when you tick the builtin. For example:
*
* : example-1 ['] ls postpone literal ; immediate
* : example-2 example-1 execute ; immediate
* : problem example-2 ;
* example-2
*
* We have no way, when we get EXECUTEd, of knowing what our behavior
* should be. Thus, our only alternative is to "outlaw" this. See RFI
* 0007, and ANS Forth Standard's appendix D, item 6.7 for a related
* problem, concerning compile semantics.
*
* The problem is compounded by the fact that "' builtin CATCH" is valid
* and desirable. The only solution is to create an intermediary word.
* For example:
*
* : my-ls ls ;
* : example ['] my-ls catch ;
*
* So, with the below implementation, here is a summary of the behavior
* of builtins:
*
* ls -l \ "interpret" behavior, ie,
* \ takes parameters from TIB
* : ex-1 s" -l" 1 ls ; \ "compile" behavior, ie,
* \ takes parameters from the stack
* : ex-2 ['] ls catch ; immediate \ undefined behavior
* : ex-3 ['] ls catch ; \ undefined behavior
* ex-2 ex-3 \ "interpret" behavior,
* \ catch works
* : ex-4 ex-2 ; \ "compile" behavior,
* \ catch does not work
* : ex-5 ex-3 ; immediate \ same as ex-2
* : ex-6 ex-3 ; \ same as ex-3
* : ex-7 ['] ex-1 catch ; \ "compile" behavior,
* \ catch works
* : ex-8 postpone ls ; immediate \ same as ex-2
* : ex-9 postpone ls ; \ same as ex-3
*
* As the definition below is particularly tricky, and it's side effects
* must be well understood by those playing with it, I'll be heavy on
* the comments.
*
* (if you edit this definition, pay attention to trailing spaces after
* each word -- I warned you! :-) )
*/
#define BUILTIN_CONSTRUCTOR \
": builtin: " \
">in @ " /* save the tib index pointer */ \
"' " /* get next word's xt */ \
"swap >in ! " /* point again to next word */ \
"create " /* create a new definition of the next word */ \
", " /* save previous definition's xt */ \
"immediate " /* make the new definition an immediate word */ \
\
"does> " /* Now, the *new* definition will: */ \
"state @ if " /* if in compiling state: */ \
"1 postpone literal " /* pass 1 flag to indicate compile */ \
"@ compile, " /* compile in previous definition */ \
"postpone throw " /* throw stack-returned result */ \
"else " /* if in interpreting state: */ \
"0 swap " /* pass 0 flag to indicate interpret */ \
"@ execute " /* call previous definition */ \
"throw " /* throw stack-returned result */ \
"then ; "
extern int ficlExecFD(ficlVm *, int);
/*
* Initialise the Forth interpreter, create all our commands as words.
*/
ficlVm *
bf_init(const char *rc, ficlOutputFunction out)
{
struct bootblk_command **cmdp;
char create_buf[41]; /* 31 characters-long builtins */
char *buf;
int fd, rv;
ficlSystemInformation *fsi;
ficlDictionary *dict;
ficlDictionary *env;
fsi = malloc(sizeof (ficlSystemInformation));
ficlSystemInformationInitialize(fsi);
fsi->textOut = out;
fsi->dictionarySize = BF_DICTSIZE;
bf_sys = ficlSystemCreate(fsi);
free(fsi);
ficlSystemCompileExtras(bf_sys);
bf_vm = ficlSystemCreateVm(bf_sys);
buf = isadir();
if (buf == NULL || strcmp(buf, "amd64") != 0) {
(void) setenv("ISADIR", "", 1);
} else {
(void) setenv("ISADIR", buf, 1);
}
if (buf != NULL)
free(buf);
buf = get_currdev();
(void) setenv("currdev", buf, 1);
free(buf);
(void) setenv("console", "text", 1);
/* Put all private definitions in a "builtins" vocabulary */
rv = ficlVmEvaluate(bf_vm,
"vocabulary builtins also builtins definitions");
if (rv != FICL_VM_STATUS_OUT_OF_TEXT) {
printf("error interpreting forth: %d\n", rv);
exit(1);
}
/* Builtin constructor word */
rv = ficlVmEvaluate(bf_vm, BUILTIN_CONSTRUCTOR);
if (rv != FICL_VM_STATUS_OUT_OF_TEXT) {
printf("error interpreting forth: %d\n", rv);
exit(1);
}
/* make all commands appear as Forth words */
dict = ficlSystemGetDictionary(bf_sys);
cmdp = NULL;
SET_FOREACH(cmdp, Xcommand_set) {
(void) ficlDictionaryAppendPrimitive(dict,
(char *)(*cmdp)->c_name, bf_command, FICL_WORD_DEFAULT);
rv = ficlVmEvaluate(bf_vm, "forth definitions builtins");
if (rv != FICL_VM_STATUS_OUT_OF_TEXT) {
printf("error interpreting forth: %d\n", rv);
exit(1);
}
(void) snprintf(create_buf, sizeof (create_buf), "builtin: %s",
(*cmdp)->c_name);
rv = ficlVmEvaluate(bf_vm, create_buf);
if (rv != FICL_VM_STATUS_OUT_OF_TEXT) {
printf("error interpreting forth: %d\n", rv);
exit(1);
}
rv = ficlVmEvaluate(bf_vm, "builtins definitions");
if (rv != FICL_VM_STATUS_OUT_OF_TEXT) {
printf("error interpreting forth: %d\n", rv);
exit(1);
}
}
rv = ficlVmEvaluate(bf_vm, "only forth definitions");
if (rv != FICL_VM_STATUS_OUT_OF_TEXT) {
printf("error interpreting forth: %d\n", rv);
exit(1);
}
/*
* Export some version numbers so that code can detect the
* loader/host version
*/
env = ficlSystemGetEnvironment(bf_sys);
(void) ficlDictionarySetConstant(env, "loader_version",
(bootprog_rev[0] - '0') * 10 + (bootprog_rev[2] - '0'));
/* try to load and run init file if present */
if (rc == NULL)
rc = "/boot/forth/boot.4th";
if (*rc != '\0') {
fd = open(rc, O_RDONLY);
if (fd != -1) {
(void) ficlExecFD(bf_vm, fd);
(void) close(fd);
}
}
gfx_framework_init();
return (bf_vm);
}
void
bf_fini(void)
{
ficlSystemDestroy(bf_sys);
gfx_framework_fini();
}
/*
* Feed a line of user input to the Forth interpreter
*/
int
bf_run(char *line)
{
int result;
ficlString s;
FICL_STRING_SET_FROM_CSTRING(s, line);
result = ficlVmExecuteString(bf_vm, s);
switch (result) {
case FICL_VM_STATUS_OUT_OF_TEXT:
case FICL_VM_STATUS_ABORTQ:
case FICL_VM_STATUS_QUIT:
case FICL_VM_STATUS_ERROR_EXIT:
break;
case FICL_VM_STATUS_USER_EXIT:
break;
case FICL_VM_STATUS_ABORT:
printf("Aborted!\n");
break;
case BF_PARSE:
printf("Parse error!\n");
break;
default:
if (command_errmsg != NULL) {
printf("%s\n", command_errmsg);
command_errmsg = NULL;
}
}
(void) setenv("interpret", bf_vm->state ? "" : "ok", 1);
return (result);
}
char *
get_dev(const char *path)
{
FILE *fp;
struct mnttab mpref = {0};
struct mnttab mp = {0};
char *currdev;
int ret;
char *buf;
char *tmppath;
char *tmpdev;
char *cwd = NULL;
fp = fopen(MNTTAB, "r");
/* do the best we can to return something... */
if (fp == NULL)
return (strdup(path));
/*
* the path can have device provided, check for it
* and extract it.
*/
buf = strrchr(path, ':');
if (buf != NULL) {
tmppath = buf+1; /* real path */
buf = strchr(path, ':'); /* skip zfs: */
buf++;
tmpdev = strdup(buf);
buf = strchr(tmpdev, ':'); /* get ending : */
*buf = '\0';
} else {
tmppath = (char *)path;
if (tmppath[0] != '/')
if ((cwd = getcwd(NULL, PATH_MAX)) == NULL) {
(void) fclose(fp);
return (strdup(path));
}
currdev = getenv("currdev");
buf = strchr(currdev, ':'); /* skip zfs: */
if (buf == NULL) {
(void) fclose(fp);
return (strdup(path));
}
buf++;
tmpdev = strdup(buf);
buf = strchr(tmpdev, ':'); /* get ending : */
*buf = '\0';
}
mpref.mnt_special = tmpdev;
ret = getmntany(fp, &mp, &mpref);
(void) fclose(fp);
free(tmpdev);
if (cwd == NULL)
(void) asprintf(&buf, "%s/%s", ret? "":mp.mnt_mountp, tmppath);
else {
(void) asprintf(&buf, "%s/%s/%s", ret? "":mp.mnt_mountp, cwd,
tmppath);
free(cwd);
}
return (buf);
}
static void
ngets(char *buf, int n)
{
int c;
char *lp;
for (lp = buf; ; )
switch (c = getchar() & 0177) {
case '\n':
case '\r':
*lp = '\0';
(void) putchar('\n');
return;
case '\b':
case '\177':
if (lp > buf) {
lp--;
(void) putchar('\b');
(void) putchar(' ');
(void) putchar('\b');
}
break;
case 'r'&037: {
char *p;
(void) putchar('\n');
for (p = buf; p < lp; ++p)
(void) putchar(*p);
break;
}
case 'u'&037:
case 'w'&037:
lp = buf;
(void) putchar('\n');
break;
default:
if ((n < 1) || ((lp - buf) < n - 1)) {
*lp++ = c;
(void) putchar(c);
}
}
/*NOTREACHED*/
}
static int
fgetstr(char *buf, int size, int fd)
{
char c;
int err, len;
size--; /* leave space for terminator */
len = 0;
while (size != 0) {
err = read(fd, &c, sizeof (c));
if (err < 0) /* read error */
return (-1);
if (err == 0) { /* EOF */
if (len == 0)
return (-1); /* nothing to read */
break;
}
if ((c == '\r') || (c == '\n')) /* line terminators */
break;
*buf++ = c; /* keep char */
size--;
len++;
}
*buf = 0;
return (len);
}
static char *
unargv(int argc, char *argv[])
{
size_t hlong;
int i;
char *cp;
for (i = 0, hlong = 0; i < argc; i++)
hlong += strlen(argv[i]) + 2;
if (hlong == 0)
return (NULL);
cp = malloc(hlong);
cp[0] = 0;
for (i = 0; i < argc; i++) {
(void) strcat(cp, argv[i]);
if (i < (argc - 1))
(void) strcat(cp, " ");
}
return (cp);
}
/*
* Help is read from a formatted text file.
*
* Entries in the file are formatted as:
* # Ttopic [Ssubtopic] Ddescription
* help
* text
* here
* #
*
* Note that for code simplicity's sake, the above format must be followed
* exactly.
*
* Subtopic entries must immediately follow the topic (this is used to
* produce the listing of subtopics).
*
* If no argument(s) are supplied by the user, the help for 'help' is displayed.
*/
static int
help_getnext(int fd, char **topic, char **subtopic, char **desc)
{
char line[81], *cp, *ep;
*topic = *subtopic = *desc = NULL;
for (;;) {
if (fgetstr(line, 80, fd) < 0)
return (0);
if (strlen(line) < 3 || line[0] != '#' || line[1] != ' ')
continue;
*topic = *subtopic = *desc = NULL;
cp = line + 2;
while (cp != NULL && *cp != 0) {
ep = strchr(cp, ' ');
if (*cp == 'T' && *topic == NULL) {
if (ep != NULL)
*ep++ = 0;
*topic = strdup(cp + 1);
} else if (*cp == 'S' && *subtopic == NULL) {
if (ep != NULL)
*ep++ = 0;
*subtopic = strdup(cp + 1);
} else if (*cp == 'D') {
*desc = strdup(cp + 1);
ep = NULL;
}
cp = ep;
}
if (*topic == NULL) {
free(*subtopic);
free(*desc);
continue;
}
return (1);
}
}
static int
help_emitsummary(char *topic, char *subtopic, char *desc)
{
int i;
(void) pager_output(" ");
(void) pager_output(topic);
i = strlen(topic);
if (subtopic != NULL) {
(void) pager_output(" ");
(void) pager_output(subtopic);
i += strlen(subtopic) + 1;
}
if (desc != NULL) {
do {
(void) pager_output(" ");
} while (i++ < 30);
(void) pager_output(desc);
}
return (pager_output("\n"));
}
COMMAND_SET(help, "help", "detailed help", command_help);
static int
command_help(int argc, char *argv[])
{
char buf[81]; /* XXX buffer size? */
int hfd, matched, doindex;
char *topic, *subtopic, *t, *s, *d;
/* page the help text from our load path */
(void) snprintf(buf, sizeof (buf), "/boot/loader.help");
if ((hfd = open(buf, O_RDONLY)) < 0) {
printf("Verbose help not available, "
"use '?' to list commands\n");
return (CMD_OK);
}
/* pick up request from arguments */
topic = subtopic = NULL;
switch (argc) {
case 3:
subtopic = strdup(argv[2]);
/* FALLTHROUGH */
case 2:
topic = strdup(argv[1]);
break;
case 1:
topic = strdup("help");
break;
default:
command_errmsg = "usage is 'help <topic> [<subtopic>]";
(void) close(hfd);
return (CMD_ERROR);
}
/* magic "index" keyword */
doindex = strcmp(topic, "index") == 0;
matched = doindex;
/* Scan the helpfile looking for help matching the request */
pager_open();
while (help_getnext(hfd, &t, &s, &d)) {
if (doindex) { /* dink around formatting */
if (help_emitsummary(t, s, d))
break;
} else if (strcmp(topic, t)) {
/* topic mismatch */
/* nothing more on this topic, stop scanning */
if (matched)
break;
} else {
/* topic matched */
matched = 1;
if ((subtopic == NULL && s == NULL) ||
(subtopic != NULL && s != NULL &&
strcmp(subtopic, s) == 0)) {
/* exact match, print text */
while (fgetstr(buf, 80, hfd) >= 0 &&
buf[0] != '#') {
if (pager_output(buf))
break;
if (pager_output("\n"))
break;
}
} else if (subtopic == NULL && s != NULL) {
/* topic match, list subtopics */
if (help_emitsummary(t, s, d))
break;
}
}
free(t);
free(s);
free(d);
t = s = d = NULL;
}
free(t);
free(s);
free(d);
pager_close();
(void) close(hfd);
if (!matched) {
(void) snprintf(command_errbuf, sizeof (command_errbuf),
"no help available for '%s'", topic);
free(topic);
free(subtopic);
return (CMD_ERROR);
}
free(topic);
free(subtopic);
return (CMD_OK);
}
COMMAND_SET(commandlist, "?", "list commands", command_commandlist);
static int
command_commandlist(int argc __unused, char *argv[] __unused)
{
struct bootblk_command **cmdp;
int res;
char name[20];
res = 0;
pager_open();
res = pager_output("Available commands:\n");
SET_FOREACH(cmdp, Xcommand_set) {
if (res)
break;
if ((*cmdp)->c_name != NULL && (*cmdp)->c_desc != NULL) {
(void) snprintf(name, sizeof (name), " %-15s ",
(*cmdp)->c_name);
(void) pager_output(name);
(void) pager_output((*cmdp)->c_desc);
res = pager_output("\n");
}
}
pager_close();
return (CMD_OK);
}
/*
* XXX set/show should become set/echo if we have variable
* substitution happening.
*/
COMMAND_SET(show, "show", "show variable(s)", command_show);
COMMAND_SET(printenv, "printenv", "show variable(s)", command_show);
static int
command_show(int argc, char *argv[])
{
char **ev;
char *cp;
if (argc < 2) {
/*
* With no arguments, print everything.
*/
pager_open();
for (ev = _environ; *ev != NULL; ev++) {
(void) pager_output(*ev);
cp = getenv(*ev);
if (cp != NULL) {
(void) pager_output("=");
(void) pager_output(cp);
}
if (pager_output("\n"))
break;
}
pager_close();
} else {
if ((cp = getenv(argv[1])) != NULL) {
printf("%s\n", cp);
} else {
(void) snprintf(command_errbuf, sizeof (command_errbuf),
"variable '%s' not found", argv[1]);
return (CMD_ERROR);
}
}
return (CMD_OK);
}
COMMAND_SET(set, "set", "set a variable", command_set);
static int
command_set(int argc, char *argv[])
{
int err;
char *value, *copy;
if (argc != 2) {
command_errmsg = "wrong number of arguments";
return (CMD_ERROR);
} else {
copy = strdup(argv[1]);
if (copy == NULL) {
command_errmsg = strerror(errno);
return (CMD_ERROR);
}
if ((value = strchr(copy, '=')) != NULL)
*(value++) = 0;
else
value = "";
if ((err = setenv(copy, value, 1)) != 0) {
free(copy);
command_errmsg = strerror(errno);
return (CMD_ERROR);
}
free(copy);
}
return (CMD_OK);
}
COMMAND_SET(setprop, "setprop", "set a variable", command_setprop);
static int
command_setprop(int argc, char *argv[])
{
int err;
if (argc != 3) {
command_errmsg = "wrong number of arguments";
return (CMD_ERROR);
} else {
if ((err = setenv(argv[1], argv[2], 1)) != 0) {
command_errmsg = strerror(err);
return (CMD_ERROR);
}
}
return (CMD_OK);
}
COMMAND_SET(unset, "unset", "unset a variable", command_unset);
static int
command_unset(int argc, char *argv[])
{
int err;
if (argc != 2) {
command_errmsg = "wrong number of arguments";
return (CMD_ERROR);
} else {
if ((err = unsetenv(argv[1])) != 0) {
command_errmsg = strerror(err);
return (CMD_ERROR);
}
}
return (CMD_OK);
}
COMMAND_SET(echo, "echo", "echo arguments", command_echo);
static int
command_echo(int argc, char *argv[])
{
char *s;
int nl, ch;
nl = 0;
optind = 1;
opterr = 1;
while ((ch = getopt(argc, argv, "n")) != -1) {
switch (ch) {
case 'n':
nl = 1;
break;
case '?':
default:
/* getopt has already reported an error */
return (CMD_OK);
}
}
argv += (optind);
argc -= (optind);
s = unargv(argc, argv);
if (s != NULL) {
printf("%s", s);
free(s);
}
if (!nl)
printf("\n");
return (CMD_OK);
}
/*
* A passable emulation of the sh(1) command of the same name.
*/
static int
ischar(void)
{
return (1);
}
COMMAND_SET(read, "read", "read input from the terminal", command_read);
static int
command_read(int argc, char *argv[])
{
char *prompt;
int timeout;
time_t when;
char *cp;
char *name;
char buf[256]; /* XXX size? */
int c;
timeout = -1;
prompt = NULL;
optind = 1;
opterr = 1;
while ((c = getopt(argc, argv, "p:t:")) != -1) {
switch (c) {
case 'p':
prompt = optarg;
break;
case 't':
timeout = strtol(optarg, &cp, 0);
if (cp == optarg) {
(void) snprintf(command_errbuf,
sizeof (command_errbuf),
"bad timeout '%s'", optarg);
return (CMD_ERROR);
}
break;
default:
return (CMD_OK);
}
}
argv += (optind);
argc -= (optind);
name = (argc > 0) ? argv[0]: NULL;
if (prompt != NULL)
printf("%s", prompt);
if (timeout >= 0) {
when = time(NULL) + timeout;
while (!ischar())
if (time(NULL) >= when)
return (CMD_OK); /* is timeout an error? */
}
ngets(buf, sizeof (buf));
if (name != NULL)
(void) setenv(name, buf, 1);
return (CMD_OK);
}
/*
* File pager
*/
COMMAND_SET(more, "more", "show contents of a file", command_more);
static int
command_more(int argc, char *argv[])
{
int i;
int res;
char line[80];
char *name;
res = 0;
pager_open();
for (i = 1; (i < argc) && (res == 0); i++) {
(void) snprintf(line, sizeof (line), "*** FILE %s BEGIN ***\n",
argv[i]);
if (pager_output(line))
break;
name = get_dev(argv[i]);
res = page_file(name);
free(name);
if (!res) {
(void) snprintf(line, sizeof (line),
"*** FILE %s END ***\n", argv[i]);
res = pager_output(line);
}
}
pager_close();
if (res == 0)
return (CMD_OK);
return (CMD_ERROR);
}
static int
page_file(char *filename)
{
int result;
result = pager_file(filename);
if (result == -1) {
(void) snprintf(command_errbuf, sizeof (command_errbuf),
"error showing %s", filename);
}
return (result);
}
COMMAND_SET(ls, "ls", "list files", command_ls);
static int
command_ls(int argc, char *argv[])
{
DIR *dir;
int fd;
struct stat sb;
struct dirent *d;
char *buf, *path;
char lbuf[128]; /* one line */
int result, ch;
int verbose;
result = CMD_OK;
fd = -1;
verbose = 0;
optind = 1;
opterr = 1;
while ((ch = getopt(argc, argv, "l")) != -1) {
switch (ch) {
case 'l':
verbose = 1;
break;
case '?':
default:
/* getopt has already reported an error */
return (CMD_OK);
}
}
argv += (optind - 1);
argc -= (optind - 1);
if (argc < 2) {
path = "";
} else {
path = argv[1];
}
fd = ls_getdir(&path);
if (fd == -1) {
result = CMD_ERROR;
goto out;
}
dir = fdopendir(fd);
pager_open();
(void) pager_output(path);
(void) pager_output("\n");
while ((d = readdir(dir)) != NULL) {
if (strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) {
/* stat the file, if possible */
if (path[0] == '\0') {
(void) asprintf(&buf, "%s", d->d_name);
} else {
(void) asprintf(&buf, "%s/%s", path, d->d_name);
}
if (buf != NULL) {
/* ignore return, could be symlink, etc. */
if (stat(buf, &sb)) {
sb.st_size = 0;
sb.st_mode = 0;
}
free(buf);
}
if (verbose) {
(void) snprintf(lbuf, sizeof (lbuf),
" %c %8d %s\n",
typestr[sb.st_mode >> 12],
(int)sb.st_size, d->d_name);
} else {
(void) snprintf(lbuf, sizeof (lbuf),
" %c %s\n",
typestr[sb.st_mode >> 12], d->d_name);
}
if (pager_output(lbuf))
goto out;
}
}
out:
pager_close();
if (fd != -1)
(void) closedir(dir);
if (path != NULL)
free(path);
return (result);
}
/*
* Given (path) containing a vaguely reasonable path specification, return an fd
* on the directory, and an allocated copy of the path to the directory.
*/
static int
ls_getdir(char **pathp)
{
struct stat sb;
int fd;
char *cp, *path;
fd = -1;
/* one extra byte for a possible trailing slash required */
path = malloc(strlen(*pathp) + 2);
(void) strcpy(path, *pathp);
/* Make sure the path is respectable to begin with */
if ((cp = get_dev(path)) == NULL) {
(void) snprintf(command_errbuf, sizeof (command_errbuf),
"bad path '%s'", path);
goto out;
}
/* If there's no path on the device, assume '/' */
if (*cp == 0)
(void) strcat(path, "/");
fd = open(cp, O_RDONLY);
if (fd < 0) {
(void) snprintf(command_errbuf, sizeof (command_errbuf),
"open '%s' failed: %s", path, strerror(errno));
goto out;
}
if (fstat(fd, &sb) < 0) {
(void) snprintf(command_errbuf, sizeof (command_errbuf),
"stat failed: %s", strerror(errno));
goto out;
}
if (!S_ISDIR(sb.st_mode)) {
(void) snprintf(command_errbuf, sizeof (command_errbuf),
"%s: %s", path, strerror(ENOTDIR));
goto out;
}
free(cp);
*pathp = path;
return (fd);
out:
free(cp);
free(path);
*pathp = NULL;
if (fd != -1)
(void) close(fd);
return (-1);
}
COMMAND_SET(include, "include", "read commands from a file", command_include);
static int
command_include(int argc, char *argv[])
{
int i;
int res;
char **argvbuf;
/*
* Since argv is static, we need to save it here.
*/
argvbuf = (char **)calloc(argc, sizeof (char *));
for (i = 0; i < argc; i++)
argvbuf[i] = strdup(argv[i]);
res = CMD_OK;
for (i = 1; (i < argc) && (res == CMD_OK); i++)
res = include(argvbuf[i]);
for (i = 0; i < argc; i++)
free(argvbuf[i]);
free(argvbuf);
return (res);
}
/*
* Header prepended to each line. The text immediately follows the header.
* We try to make this short in order to save memory -- the loader has
* limited memory available, and some of the forth files are very long.
*/
struct includeline
{
struct includeline *next;
int line;
char text[];
};
int
include(const char *filename)
{
struct includeline *script, *se, *sp;
int res = CMD_OK;
int prevsrcid, fd, line;
char *cp, input[256]; /* big enough? */
char *path;
path = get_dev(filename);
if (((fd = open(path, O_RDONLY)) == -1)) {
(void) snprintf(command_errbuf, sizeof (command_errbuf),
"can't open '%s': %s", filename,
strerror(errno));
free(path);
return (CMD_ERROR);
}
free(path);
/*
* Read the script into memory.
*/
script = se = NULL;
line = 0;
while (fgetstr(input, sizeof (input), fd) >= 0) {
line++;
cp = input;
/* Allocate script line structure and copy line, flags */
if (*cp == '\0')
continue; /* ignore empty line, save memory */
if (cp[0] == '\\' && cp[1] == ' ')
continue; /* ignore comment */
sp = malloc(sizeof (struct includeline) + strlen(cp) + 1);
/*
* On malloc failure (it happens!), free as much as possible
* and exit
*/
if (sp == NULL) {
while (script != NULL) {
se = script;
script = script->next;
free(se);
}
(void) snprintf(command_errbuf, sizeof (command_errbuf),
"file '%s' line %d: memory allocation "
"failure - aborting", filename, line);
return (CMD_ERROR);
}
(void) strcpy(sp->text, cp);
sp->line = line;
sp->next = NULL;
if (script == NULL) {
script = sp;
} else {
se->next = sp;
}
se = sp;
}
(void) close(fd);
/*
* Execute the script
*/
prevsrcid = bf_vm->sourceId.i;
bf_vm->sourceId.i = fd+1; /* 0 is user input device */
res = CMD_OK;
for (sp = script; sp != NULL; sp = sp->next) {
res = bf_run(sp->text);
if (res != FICL_VM_STATUS_OUT_OF_TEXT) {
(void) snprintf(command_errbuf, sizeof (command_errbuf),
"Error while including %s, in the line %d:\n%s",
filename, sp->line, sp->text);
res = CMD_ERROR;
break;
} else
res = CMD_OK;
}
bf_vm->sourceId.i = -1;
(void) bf_run("");
bf_vm->sourceId.i = prevsrcid;
while (script != NULL) {
se = script;
script = script->next;
free(se);
}
return (res);
}
COMMAND_SET(boot, "boot", "boot a file or loaded kernel", command_boot);
static int
command_boot(int argc, char *argv[])
{
return (CMD_OK);
}
COMMAND_SET(autoboot, "autoboot", "boot automatically after a delay",
command_autoboot);
static int
command_autoboot(int argc, char *argv[])
{
return (CMD_OK);
}
static void
moduledir_rebuild(void)
{
struct moduledir *mdp, *mtmp;
const char *path, *cp, *ep;
int cplen;
path = getenv("module_path");
if (path == NULL)
path = default_searchpath;
/*
* Rebuild list of module directories if it changed
*/
STAILQ_FOREACH(mdp, &moduledir_list, d_link)
mdp->d_flags |= MDIR_REMOVED;
for (ep = path; *ep != 0; ep++) {
cp = ep;
for (; *ep != 0 && *ep != ';'; ep++)
;
/*
* Ignore trailing slashes
*/
for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/';
cplen--)
;
STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
if (strlen(mdp->d_path) != cplen ||
bcmp(cp, mdp->d_path, cplen) != 0)
continue;
mdp->d_flags &= ~MDIR_REMOVED;
break;
}
if (mdp == NULL) {
mdp = malloc(sizeof (*mdp) + cplen + 1);
if (mdp == NULL)
return;
mdp->d_path = (char *)(mdp + 1);
bcopy(cp, mdp->d_path, cplen);
mdp->d_path[cplen] = 0;
mdp->d_hints = NULL;
mdp->d_flags = 0;
STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link);
}
if (*ep == 0)
break;
}
/*
* Delete unused directories if any
*/
mdp = STAILQ_FIRST(&moduledir_list);
while (mdp) {
if ((mdp->d_flags & MDIR_REMOVED) == 0) {
mdp = STAILQ_NEXT(mdp, d_link);
} else {
if (mdp->d_hints)
free(mdp->d_hints);
mtmp = mdp;
mdp = STAILQ_NEXT(mdp, d_link);
STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link);
free(mtmp);
}
}
}
static char *
file_lookup(const char *path, const char *name, int namelen)
{
struct stat st;
char *result, *cp, *gz;
int pathlen;
pathlen = strlen(path);
result = malloc(pathlen + namelen + 2);
if (result == NULL)
return (NULL);
bcopy(path, result, pathlen);
if (pathlen > 0 && result[pathlen - 1] != '/')
result[pathlen++] = '/';
cp = result + pathlen;
bcopy(name, cp, namelen);
cp += namelen;
*cp = '\0';
if (stat(result, &st) == 0 && S_ISREG(st.st_mode))
return (result);
/* also check for gz file */
(void) asprintf(&gz, "%s.gz", result);
if (gz != NULL) {
int res = stat(gz, &st);
free(gz);
if (res == 0)
return (result);
}
free(result);
return (NULL);
}
static char *
file_search(const char *name)
{
struct moduledir *mdp;
struct stat sb;
char *result;
int namelen;
if (name == NULL)
return (NULL);
if (*name == 0)
return (strdup(name));
if (strchr(name, '/') != NULL) {
char *gz;
if (stat(name, &sb) == 0)
return (strdup(name));
/* also check for gz file */
(void) asprintf(&gz, "%s.gz", name);
if (gz != NULL) {
int res = stat(gz, &sb);
free(gz);
if (res == 0)
return (strdup(name));
}
return (NULL);
}
moduledir_rebuild();
result = NULL;
namelen = strlen(name);
STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
result = file_lookup(mdp->d_path, name, namelen);
if (result)
break;
}
return (result);
}
COMMAND_SET(load, "load", "load a kernel or module", command_load);
static int
command_load(int argc, char *argv[])
{
int dofile, ch;
char *typestr = NULL;
char *filename;
dofile = 0;
optind = 1;
if (argc == 1) {
command_errmsg = "no filename specified";
return (CMD_ERROR);
}
while ((ch = getopt(argc, argv, "kt:")) != -1) {
switch (ch) {
case 'k':
break;
case 't':
typestr = optarg;
dofile = 1;
break;
case '?':
default:
return (CMD_OK);
}
}
argv += (optind - 1);
argc -= (optind - 1);
if (dofile) {
if ((typestr == NULL) || (*typestr == 0)) {
command_errmsg = "invalid load type";
return (CMD_ERROR);
}
#if 0
return (file_loadraw(argv[1], typestr, argc - 2, argv + 2, 1)
? CMD_OK : CMD_ERROR);
#endif
return (CMD_OK);
}
filename = file_search(argv[1]);
if (filename == NULL) {
(void) snprintf(command_errbuf, sizeof (command_errbuf),
"can't find '%s'", argv[1]);
return (CMD_ERROR);
}
(void) setenv("kernelname", filename, 1);
return (CMD_OK);
}
COMMAND_SET(unload, "unload", "unload all modules", command_unload);
static int
command_unload(int argc, char *argv[])
{
(void) unsetenv("kernelname");
return (CMD_OK);
}
COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
static int
command_reboot(int argc, char *argv[])
{
exit(0);
return (CMD_OK);
}
COMMAND_SET(sifting, "sifting", "find words", command_sifting);
static int
command_sifting(int argc, char *argv[])
{
if (argc != 2) {
command_errmsg = "wrong number of arguments";
return (CMD_ERROR);
}
ficlPrimitiveSiftingImpl(bf_vm, argv[1]);
return (CMD_OK);
}
/* Only implement get and list. Ignore arguments on, off and set. */
COMMAND_SET(framebuffer, "framebuffer", "framebuffer mode management",
command_framebuffer);
static int
command_framebuffer(int argc, char *argv[])
{
if (fb.fd < 0) {
printf("Framebuffer is not available.\n");
return (CMD_OK);
}
if (argc == 2 && strcmp(argv[1], "get") == 0) {
printf("\nSystem frame buffer: %s\n", fb.ident.name);
printf("%dx%dx%d, stride=%d\n", fb.fb_width, fb.fb_height,
fb.fb_depth, (fb.fb_pitch << 3) / fb.fb_depth);
return (CMD_OK);
}
if (argc == 2 && strcmp(argv[1], "list") == 0) {
printf("0: %dx%dx%d\n", fb.fb_width, fb.fb_height, fb.fb_depth);
return (CMD_OK);
}
if (argc == 3 && strcmp(argv[1], "set") == 0)
return (CMD_OK);
if (argc == 2 && strcmp(argv[1], "on") == 0)
return (CMD_OK);
if (argc == 2 && strcmp(argv[1], "off") == 0)
return (CMD_OK);
(void) snprintf(command_errbuf, sizeof (command_errbuf),
"usage: %s get | list", argv[0]);
return (CMD_ERROR);
}
/*
* 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 2015 Toomas Soome <tsoome@me.com>
*/
#ifndef _LOADER_EMU_H
#define _LOADER_EMU_H
#include <sys/linker_set.h>
/*
* BootFORTH emulator interface.
*/
#ifdef __cplusplus
extern "C" {
#endif
/* Commands and return values; nonzero return sets command_errmsg != NULL */
typedef int (bootblk_cmd_t)(int argc, char *argv[]);
extern char *command_errmsg;
extern char command_errbuf[]; /* XXX blah, length */
#define CMD_OK 0
#define CMD_ERROR 1
/*
* Support for commands
*/
struct bootblk_command
{
const char *c_name;
const char *c_desc;
bootblk_cmd_t *c_fn;
};
#define COMMAND_SET(tag, key, desc, func) \
static bootblk_cmd_t func; \
static struct bootblk_command _cmd_ ## tag = { key, desc, func }; \
DATA_SET(Xcommand_set, _cmd_ ## tag)
SET_DECLARE(Xcommand_set, struct bootblk_command);
#ifdef __cplusplus
}
#endif
#endif /* _LOADER_EMU_H */
|