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
|
/*
* 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) 2011 Gary Mills
*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/types.h>
#include <ctype.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <libintl.h>
#include <locale.h>
#include <sys/fdio.h>
#include <sys/dktp/fdisk.h>
#include <sys/dkio.h>
#include <sys/sysmacros.h>
#include "mkfs_pcfs.h"
#include <sys/fs/pc_fs.h>
#include <sys/fs/pc_dir.h>
#include <sys/fs/pc_label.h>
#include <macros.h>
/*
* mkfs (for pcfs)
*
* Install a boot block, FAT, and (if desired) the first resident
* of the new fs.
*
* XXX -- floppy opens need O_NDELAY?
*/
#define IN_RANGE(n, x, y) (((n) >= (x)) && ((n) <= (y)))
#define DEFAULT_LABEL "NONAME"
static char *BootBlkFn = NULL;
static char *DiskName = NULL;
static char *FirstFn = NULL;
static char *Label = NULL;
static char Firstfileattr = 0x20;
static int Outputtofile = 0;
static int SunBPBfields = 0;
static int GetFsParams = 0;
static int Fatentsize = 0;
static int Imagesize = 3;
static int Notreally = 0;
static int Verbose = 0;
static int MakeFAT32 = 0;
/*
* If there is an FDISK entry for the device where we're about to
* make the file system, we ought to make a file system that has the
* same size FAT as the FDISK table claims. We track the size FDISK
* thinks in this variable.
*/
static int FdiskFATsize = 0;
static int GetSize = 1; /* Unless we're given as arg, must look it up */
static ulong_t TotSize; /* Total size of FS in # of sectors */
static int GetSPC = 1; /* Unless we're given as arg, must calculate */
static ulong_t SecPerClust; /* # of sectors per cluster */
static int GetOffset = 1; /* Unless we're given as arg, must look it up */
static ulong_t RelOffset; /* Relative start sector (hidden sectors) */
static int GetSPT = 1; /* Unless we're given as arg, must look it up */
static ushort_t SecPerTrk; /* # of sectors per track */
static int GetTPC = 1; /* Unless we're given as arg, must look it up */
static ushort_t TrkPerCyl; /* # of tracks per cylinder */
static int GetResrvd = 1; /* Unless we're given as arg, must calculate */
static int Resrvd; /* Number of reserved sectors */
static int GetBPF = 1; /* Unless we're given as arg, must calculate */
static int BitsPerFAT; /* Total size of FS in # of sectors */
static ulong_t TotalClusters; /* Computed total number of clusters */
/*
* Unless we are told otherwise, we should use fdisk table for non-diskettes.
*/
static int DontUseFdisk = 0;
/*
* Function prototypes
*/
#ifndef i386
static void swap_pack_grabsebpb(bpb_t *wbpb, struct _boot_sector *bsp);
static void swap_pack_bpb32cpy(struct _boot_sector32 *bsp, bpb_t *wbpb);
static void swap_pack_sebpbcpy(struct _boot_sector *bsp, bpb_t *wbpb);
static void swap_pack_grabbpb(bpb_t *wbpb, struct _boot_sector *bsp);
static void swap_pack_bpbcpy(struct _boot_sector *bsp, bpb_t *wbpb);
#endif
static uchar_t *build_rootdir(bpb_t *wbpb, char *ffn, int fffd,
ulong_t ffsize, pc_cluster32_t ffstart, ulong_t *rdirsize);
static uchar_t *build_fat(bpb_t *wbpb, struct fat_od_fsi *fsinfop,
ulong_t bootblksize, ulong_t *fatsize, char *ffn, int *fffd,
ulong_t *ffsize, pc_cluster32_t *ffstartclust);
static char *stat_actual_disk(char *diskname, struct stat *info, char **suffix);
static void compare_existing_with_computed(int fd, char *suffix,
bpb_t *wbpb, int *prtsize, int *prtspc, int *prtbpf, int *prtnsect,
int *prtntrk, int *prtfdisk, int *prthidden, int *prtrsrvd,
int *dashos);
static void print_reproducing_command(int fd, char *actualdisk, char *suffix,
bpb_t *wbpb);
static void compute_file_area_size(bpb_t *wbpb);
static void write_fat32_bootstuff(int fd, boot_sector_t *bsp,
struct fat_od_fsi *fsinfop, off64_t seekto);
static void sanity_check_options(int argc, int optind);
static void compute_cluster_size(bpb_t *wbpb);
static void find_fixed_details(int fd, bpb_t *wbpb);
static void dirent_fname_fill(struct pcdir *dep, char *fn);
static void floppy_bpb_fillin(bpb_t *wbpb,
int diam, int hds, int spt);
static void read_existing_bpb(int fd, bpb_t *wbpb);
static void warn_funky_fatsize(void);
static void warn_funky_floppy(void);
static void dirent_time_fill(struct pcdir *dep);
static void parse_suboptions(char *optsstr);
static void header_for_dump(void);
static void write_bootsects(int fd, boot_sector_t *bsp, bpb_t *wbpb,
struct fat_od_fsi *fsinfop, off64_t seekto);
static void fill_bpb_sizes(bpb_t *wbpb, struct ipart part[],
int partno, off64_t offset);
static void set_fat_string(bpb_t *wbpb, int fatsize);
static void partn_lecture(char *dn);
static void store_16_bits(uchar_t **bp, uint32_t v);
static void store_32_bits(uchar_t **bp, uint32_t v);
static void lookup_floppy(struct fd_char *fdchar, bpb_t *wbpb);
static void label_volume(char *lbl, bpb_t *wbpb);
static void mark_cluster(uchar_t *fatp, pc_cluster32_t clustnum,
uint32_t value);
static void missing_arg(char *option);
static void dashm_bail(int fd);
static void dump_bytes(uchar_t *, int);
static void write_rest(bpb_t *wbpb, char *efn,
int dfd, int sfd, int remaining);
static void write_fat(int fd, off64_t seekto, char *fn, char *lbl,
char *ffn, bpb_t *wbpb);
static void bad_arg(char *option);
static void usage(void);
static int prepare_image_file(char *fn, bpb_t *wbpb);
static int verify_bootblkfile(char *fn, boot_sector_t *bs,
ulong_t *blkfilesize);
static int open_and_examine(char *dn, bpb_t *wbpb);
static int verify_firstfile(char *fn, ulong_t *filesize);
static int lookup_FAT_size(uchar_t partid);
static int open_and_seek(char *dn, bpb_t *wbpb, off64_t *seekto);
static int warn_mismatch(char *desc, char *src, int expect, int assigned);
static int copy_bootblk(char *fn, boot_sector_t *bootsect,
ulong_t *bootblksize);
static int parse_drvnum(char *pn);
static int seek_nofdisk(int fd, bpb_t *wbpb, off64_t *seekto);
static int ask_nicely(char *special);
static int seek_partn(int fd, char *pn, bpb_t *wbpb, off64_t *seekto);
static int yes(void);
/*
* usage
*
* Display usage message and exit.
*/
static
void
usage(void)
{
(void) fprintf(stderr,
gettext("pcfs usage: mkfs [-F FSType] [-V] [-m] "
"[-o specific_options] special\n"));
(void) fprintf(stderr,
gettext(" -V: print this command line and return\n"
" -m: dump command line used to create a FAT on this media\n"
"\t(other options are ignored if this option is chosen).\n"
" -o: pcfs_specific_options:\n"
"\t'pcfs_specific_options' is a comma separated list\n"
"\tincluding one or more of the following options:\n"
"\t N,v,r,h,s,b=label,B=filename,i=filename,\n"
"\t spc=n,fat=n,nsect=n,ntrack=n,nofdisk,size=n,\n"
"\t reserve=n,hidden=n\n\n"));
(void) fprintf(stderr,
gettext("'Special' should specify a raw diskette "
"or raw fixed disk device. \"Fixed\"\n"
"disks (which include high-capacity removable "
"media such as Zip disks)\n"
"may be further qualified with a logical "
"drive specifier.\n"
"Examples are: /dev/rdiskette and "
"/dev/rdsk/c0t0d0p0:c\n"));
exit(1);
}
static
int
yes(void)
{
char *affirmative = gettext("yY");
char *a = affirmative;
int b;
b = getchar();
while (b == '\n' && b != '\0' && b != EOF)
b = getchar();
while (*a) {
if (b == (int)*a)
break;
a++;
}
return (*a);
}
static
int
ask_nicely(char *special)
{
/*
* 4228473 - No way to non-interactively make a pcfs filesystem
*
* If we don't have an input TTY, or we aren't really doing
* anything, then don't ask questions. Assume a yes answer
* to any questions we would ask.
*/
if (Notreally || !isatty(fileno(stdin)))
return (1);
(void) printf(
gettext("Construct a new FAT file system on %s: (y/n)? "), special);
(void) fflush(stdout);
return (yes());
}
/*
* store_16_bits
* Save the lower 16 bits of a 32 bit value (v) into the provided
* buffer (pointed at by *bp), and increment the buffer pointer
* as well. This way the routine can be called multiple times in
* succession to fill buffers. The value is stored in little-endian
* order.
*/
static
void
store_16_bits(uchar_t **bp, uint32_t v)
{
uchar_t *l = *bp;
*l++ = v & 0xff;
*l = (v >> 8) & 0xff;
*bp += 2;
}
/*
* store_32_bits
* Save the 32 bit value (v) into the provided buffer (pointed
* at by *bp), and increment the buffer pointer as well. This way
* the routine can be called multiple times in succession to fill
* buffers. The value is stored in little-endian order.
*/
static
void
store_32_bits(uchar_t **bp, uint32_t v)
{
uchar_t *l = *bp;
int b;
for (b = 0; b < 4; b++) {
*l++ = v & 0xff;
v = v >> 8;
}
*bp += 4;
}
/*
* dump_bytes -- display bytes as hex numbers.
* b is the pointer to the byte buffer
* n is the number of bytes in the buffer
*/
/* Note: BPL = bytes to display per line */
#define BPL 16
static
void
dump_bytes(uchar_t *b, int n)
{
int cd = n;
int cu = 0;
int o = 0;
int bl;
int ac;
/* Display offset, 16 bytes per line, and printable ascii version */
while (cd > 0) {
ac = 0;
(void) printf("\n%06x: ", o);
for (bl = 0; bl < BPL; bl++) {
if (cu+bl < n) {
(void) printf("%02x ", (b[cu+bl] & 0xff));
ac++;
}
else
(void) printf(" ");
}
for (bl = 0; bl < BPL; bl++) {
if ((cu+bl < n) &&
((b[cu+bl] >= ' ') && (b[cu+bl] <= '~')))
(void) printf("%c", b[cu+bl]);
else
(void) printf(".");
}
cu += ac; o += ac; cd -= ac;
}
(void) printf("\n\n");
}
/*
* header_for_dump -- display simple header over what will be output.
*/
static
void
header_for_dump(void)
{
int bl;
(void) printf("\n ");
for (bl = 0; bl < BPL; bl++)
(void) printf("%02x ", bl);
(void) printf("\n ");
bl = 3*BPL;
while (bl-- > 0)
(void) printf("-");
}
/*
* parse_drvnum
* Convert a partition name into a drive number.
*/
static
int
parse_drvnum(char *pn)
{
int drvnum;
/*
* Determine logical drive to seek after.
*/
if (strlen(pn) == 1 && *pn >= 'c' && *pn <= 'z') {
drvnum = *pn - 'c' + 1;
} else if (*pn >= '0' && *pn <= '9') {
char *d;
int v, m, c;
v = 0;
d = pn;
while (*d && *d >= '0' && *d <= '9') {
c = strlen(d);
m = 1;
while (--c)
m *= 10;
v += m * (*d - '0');
d++;
}
if (*d || v > 24) {
(void) fprintf(stderr,
gettext("%s: bogus logical drive specification.\n"),
pn);
return (-1);
}
drvnum = v;
} else if (strcmp(pn, "boot") == 0) {
drvnum = 99;
} else {
(void) fprintf(stderr,
gettext("%s: bogus logical drive specification.\n"), pn);
return (-1);
}
return (drvnum);
}
/*
* Define some special logical drives we use.
*/
#define BOOT_PARTITION_DRIVE 99
#define PRIMARY_DOS_DRIVE 1
/*
* isDosDrive()
* Boolean function. Give it the systid field for an fdisk partition
* and it decides if that's a systid that describes a DOS drive. We
* use systid values defined in sys/dktp/fdisk.h.
*/
static int
isDosDrive(uchar_t checkMe)
{
return ((checkMe == DOSOS12) || (checkMe == DOSOS16) ||
(checkMe == DOSHUGE) || (checkMe == FDISK_WINDOWS) ||
(checkMe == FDISK_EXT_WIN) || (checkMe == FDISK_FAT95) ||
(checkMe == DIAGPART));
}
/*
* isDosExtended()
* Boolean function. Give it the systid field for an fdisk partition
* and it decides if that's a systid that describes an extended DOS
* partition.
*/
static int
isDosExtended(uchar_t checkMe)
{
return ((checkMe == EXTDOS) || (checkMe == FDISK_EXTLBA));
}
/*
* isBootPart()
* Boolean function. Give it the systid field for an fdisk partition
* and it decides if that's a systid that describes a Solaris boot
* partition.
*/
static int
isBootPart(uchar_t checkMe)
{
return (checkMe == X86BOOT);
}
static
int
warn_mismatch(char *desc, char *src, int expect, int assigned)
{
if (expect == assigned)
return (assigned);
/*
* 4228473 - No way to non-interactively make a pcfs filesystem
*
* If we don't have an input TTY, or we aren't really doing
* anything, then don't ask questions. Assume a yes answer
* to any questions we would ask.
*/
if (Notreally || !isatty(fileno(stdin))) {
(void) printf(gettext("WARNING: User supplied %s is %d,"
"\nbut value obtained from the %s is %d.\n"
"Using user supplied value.\n"),
desc, assigned, src, expect);
return (assigned);
}
(void) printf(gettext("User supplied %s is %d."
"\nThe value obtained from the %s is %d.\n"),
desc, assigned, src, expect);
(void) printf(
gettext("Continue with value given on command line (y/n)? "));
(void) fflush(stdout);
if (yes())
return (assigned);
else
exit(2);
/*NOTREACHED*/
}
static
void
fill_fat32_bpb(bpb_t *wbpb)
{
/*
* ExtFlags means (according to MSDN BPB (FAT32) document)
*
* Bit 8 indicates info written to the active FAT is written
* to all copies of the FAT. (I think they mean bit 7, with
* numbering starting at 0)
*
* Lowest 4 bits of field are the 0 based FAT number of the
* Active FAT. (only meaningful if bit 8 is set)
*
* Field contains combination of these values:
*
* VALUE DESCRIPTION
* BGBPB_F_ActiveFATMsk Mask for low four bits
* (0x000F)
* BGBPB_F_NoFATMirror If set FAT mirroring disabled.
* (0x0080) If clear, FAT mirroring enabled.
*
* We set the value based on what I've seen on all the FAT32 drives
* I've seen created by Windows.
*
*/
wbpb->bpb32.ext_flags = 0x0;
/*
* No real explanation of the fs_vers file in the BPB doc. The
* high byte is supposed to be the major version and the low the
* minor version. Again I set according to what I've seen on Windows.
*/
wbpb->bpb32.fs_vers_lo = '\0';
wbpb->bpb32.fs_vers_hi = '\0';
/*
* The convention appears to be to place the fs info sector
* immediately after the boot sector, and that the backup boot
* sector should be at sector 6. (based on what I see with
* Windows)
*/
wbpb->bpb32.fsinfosec = 1;
wbpb->bpb32.backupboot = 6;
}
static
void
fill_bpb_sizes(bpb_t *wbpb, struct ipart part[], int partno, off64_t offset)
{
ulong_t usesize;
if (GetFsParams || GetSize) {
usesize = ltohi(part[partno].numsect);
if (Verbose) {
(void) printf(
gettext("Partition size (from FDISK table) "
"= %d sectors.\n"), usesize);
}
} else {
usesize = warn_mismatch(
gettext("length of partition (in sectors)"),
gettext("FDISK table"),
ltohi(part[partno].numsect), TotSize);
}
if (GetFsParams) {
TotSize = usesize;
} else {
if (usesize > 0xffff)
wbpb->bpb.sectors_in_volume = 0;
else
wbpb->bpb.sectors_in_volume = usesize;
wbpb->bpb.sectors_in_logical_volume = usesize;
}
wbpb->bpb.hidden_sectors = offset;
if (GetFsParams) {
RelOffset = offset;
} else {
wbpb->sunbpb.bs_offset_high = offset >> 16;
wbpb->sunbpb.bs_offset_low = offset & 0xFFFF;
}
}
/*
* lookup_FAT_size
*
* Given the FDISK partition file system identifier, return the
* expected FAT size for the partition.
*/
static
int
lookup_FAT_size(uchar_t partid)
{
int rval;
switch (partid) {
case DOSOS12:
rval = 12;
break;
case DOSOS16:
case DOSHUGE:
case FDISK_FAT95:
case X86BOOT:
rval = 16;
break;
case FDISK_WINDOWS:
case FDISK_EXT_WIN:
rval = 32;
break;
case EXTDOS:
case FDISK_EXTLBA:
default:
rval = -1;
break;
}
return (rval);
}
/*
* seek_partn
*
* Seek to the beginning of the partition where we need to install
* the new FAT. Zero return for any error, but print error
* messages here.
*/
static
int
seek_partn(int fd, char *pn, bpb_t *wbpb, off64_t *seekto)
{
struct ipart part[FD_NUMPART];
struct mboot extmboot;
struct mboot mb;
diskaddr_t xstartsect;
off64_t nextseek = 0;
off64_t lastseek = 0;
int logicalDriveCount = 0;
int extendedPart = -1;
int primaryPart = -1;
int bootPart = -1;
uint32_t xnumsect = 0;
int drvnum;
int driveIndex;
int i;
/*
* Count of drives in the current extended partition's
* FDISK table, and indexes of the drives themselves.
*/
int extndDrives[FD_NUMPART];
int numDrives = 0;
/*
* Count of drives (beyond primary) in master boot record's
* FDISK table, and indexes of the drives themselves.
*/
int extraDrives[FD_NUMPART];
int numExtraDrives = 0;
if ((drvnum = parse_drvnum(pn)) < 0)
return (PART_NOT_FOUND);
if (read(fd, &mb, sizeof (mb)) != sizeof (mb)) {
(void) fprintf(stderr,
gettext("Couldn't read a Master Boot Record?!\n"));
return (PART_NOT_FOUND);
}
if (ltohs(mb.signature) != BOOTSECSIG) {
(void) fprintf(stderr,
gettext("Bad Sig on master boot record!\n"));
return (PART_NOT_FOUND);
}
*seekto = 0;
/*
* Copy partition table into memory
*/
(void) memcpy(part, mb.parts, sizeof (part));
/*
* Get a summary of what is in the Master FDISK table.
* Normally we expect to find one partition marked as a DOS drive.
* This partition is the one Windows calls the primary dos partition.
* If the machine has any logical drives then we also expect
* to find a partition marked as an extended DOS partition.
*
* Sometimes we'll find multiple partitions marked as DOS drives.
* The Solaris fdisk program allows these partitions
* to be created, but Windows fdisk no longer does. We still need
* to support these, though, since Windows does. We also need to fix
* our fdisk to behave like the Windows version.
*
* It turns out that some off-the-shelf media have *only* an
* Extended partition, so we need to deal with that case as
* well.
*
* Only a single (the first) Extended or Boot Partition will
* be recognized. Any others will be ignored.
*/
for (i = 0; i < FD_NUMPART; i++) {
if (isDosDrive(part[i].systid)) {
if (primaryPart < 0) {
logicalDriveCount++;
primaryPart = i;
} else {
extraDrives[numExtraDrives++] = i;
}
continue;
}
if ((extendedPart < 0) && isDosExtended(part[i].systid)) {
extendedPart = i;
continue;
}
if ((bootPart < 0) && isBootPart(part[i].systid)) {
bootPart = i;
continue;
}
}
if (drvnum == BOOT_PARTITION_DRIVE) {
if (bootPart < 0) {
(void) fprintf(stderr,
gettext("No boot partition found on drive\n"));
return (PART_NOT_FOUND);
}
if ((*seekto = ltohi(part[bootPart].relsect)) == 0) {
(void) fprintf(stderr, gettext("Bogus FDISK entry? "
"A boot partition starting\nat sector 0 would "
"collide with the FDISK table!\n"));
return (PART_NOT_FOUND);
}
fill_bpb_sizes(wbpb, part, bootPart, *seekto);
*seekto *= BPSEC;
FdiskFATsize = lookup_FAT_size(part[bootPart].systid);
if (Verbose)
(void) printf(gettext("Boot partition's offset: "
"Sector %x.\n"), *seekto/BPSEC);
if (lseek64(fd, *seekto, SEEK_SET) < 0) {
(void) fprintf(stderr, gettext("Partition %s: "), pn);
perror("");
return (PART_NOT_FOUND);
}
return (PART_FOUND);
}
if (drvnum == PRIMARY_DOS_DRIVE && primaryPart >= 0) {
if ((*seekto = ltohi(part[primaryPart].relsect)) == 0) {
(void) fprintf(stderr, gettext("Bogus FDISK entry? "
"A partition starting\nat sector 0 would "
"collide with the FDISK table!\n"));
return (PART_NOT_FOUND);
}
fill_bpb_sizes(wbpb, part, primaryPart, *seekto);
*seekto *= BPSEC;
FdiskFATsize = lookup_FAT_size(part[primaryPart].systid);
if (Verbose)
(void) printf(gettext("Partition's offset: "
"Sector %x.\n"), *seekto/BPSEC);
if (lseek64(fd, *seekto, SEEK_SET) < 0) {
(void) fprintf(stderr, gettext("Partition %s: "), pn);
perror("");
return (PART_NOT_FOUND);
}
return (PART_FOUND);
}
/*
* We are not looking for the C: drive (or there was no primary
* drive found), so we had better have an extended partition or
* extra drives in the Master FDISK table.
*/
if ((extendedPart < 0) && (numExtraDrives == 0)) {
(void) fprintf(stderr,
gettext("No such logical drive "
"(missing extended partition entry)\n"));
return (PART_NOT_FOUND);
}
if (extendedPart >= 0) {
nextseek = xstartsect = ltohi(part[extendedPart].relsect);
xnumsect = ltohi(part[extendedPart].numsect);
do {
/*
* If the seek would not cause us to change
* position on the drive, then we're out of
* extended partitions to examine.
*/
if (nextseek == lastseek)
break;
logicalDriveCount += numDrives;
/*
* Seek the next extended partition, and find
* logical drives within it.
*/
if (lseek64(fd, nextseek * BPSEC, SEEK_SET) < 0 ||
read(fd, &extmboot, sizeof (extmboot)) !=
sizeof (extmboot)) {
perror(gettext("Unable to read extended "
"partition record"));
return (PART_NOT_FOUND);
}
(void) memcpy(part, extmboot.parts, sizeof (part));
lastseek = nextseek;
if (ltohs(extmboot.signature) != MBB_MAGIC) {
(void) fprintf(stderr,
gettext("Bad signature on "
"extended partition\n"));
return (PART_NOT_FOUND);
}
/*
* Count up drives, and track where the next
* extended partition is in case we need it. We
* are expecting only one extended partition. If
* there is more than one we'll only go to the
* first one we see, but warn about ignoring.
*/
numDrives = 0;
for (i = 0; i < FD_NUMPART; i++) {
if (isDosDrive(part[i].systid)) {
extndDrives[numDrives++] = i;
continue;
} else if (isDosExtended(part[i].systid)) {
if (nextseek != lastseek) {
/*
* Already found an extended
* partition in this table.
*/
(void) fprintf(stderr,
gettext("WARNING: "
"Ignoring unexpected "
"additional extended "
"partition"));
continue;
}
nextseek = xstartsect +
ltohi(part[i].relsect);
continue;
}
}
} while (drvnum > logicalDriveCount + numDrives);
if (drvnum <= logicalDriveCount + numDrives) {
/*
* The number of logical drives we've found thus
* far is enough to get us to the one we were
* searching for.
*/
driveIndex = logicalDriveCount + numDrives - drvnum;
*seekto =
ltohi(part[extndDrives[driveIndex]].relsect) +
lastseek;
if (*seekto == lastseek) {
(void) fprintf(stderr,
gettext("Bogus FDISK entry? A logical "
"drive starting at\nsector 0x%llx would "
"collide with the\nFDISK information in "
"that sector.\n"), *seekto);
return (PART_NOT_FOUND);
} else if (*seekto <= xstartsect ||
*seekto >= (xstartsect + xnumsect)) {
(void) fprintf(stderr,
gettext("Bogus FDISK entry? "
"Logical drive start sector (0x%llx)\n"
"not within extended partition! "
"(Expected in range 0x%x - 0x%x)\n"),
*seekto, xstartsect + 1,
xstartsect + xnumsect - 1);
return (PART_NOT_FOUND);
}
fill_bpb_sizes(wbpb, part, extndDrives[driveIndex],
*seekto);
*seekto *= BPSEC;
FdiskFATsize = lookup_FAT_size(
part[extndDrives[driveIndex]].systid);
if (Verbose)
(void) printf(gettext("Partition's offset: "
"Sector 0x%x.\n"), *seekto/BPSEC);
if (lseek64(fd, *seekto, SEEK_SET) < 0) {
(void) fprintf(stderr,
gettext("Partition %s: "), pn);
perror("");
return (PART_NOT_FOUND);
}
return (PART_FOUND);
} else {
/*
* We ran out of extended dos partition
* drives. The only hope now is to go
* back to extra drives defined in the master
* fdisk table. But we overwrote that table
* already, so we must load it in again.
*/
logicalDriveCount += numDrives;
(void) memcpy(part, mb.parts, sizeof (part));
}
}
/*
* Still haven't found the drive, is it an extra
* drive defined in the main FDISK table?
*/
if (drvnum <= logicalDriveCount + numExtraDrives) {
driveIndex = logicalDriveCount + numExtraDrives - drvnum;
*seekto = ltohi(part[extraDrives[driveIndex]].relsect);
if (*seekto == 0) {
(void) fprintf(stderr, gettext("Bogus FDISK entry? "
"A partition starting\nat sector 0 would "
"collide with the FDISK table!\n"));
return (PART_NOT_FOUND);
}
fill_bpb_sizes(wbpb, part, extraDrives[driveIndex], *seekto);
*seekto *= BPSEC;
FdiskFATsize =
lookup_FAT_size(part[extraDrives[driveIndex]].systid);
if (Verbose)
(void) printf(gettext("Partition's offset: "
"Sector %x.\n"), *seekto/BPSEC);
if (lseek64(fd, *seekto, SEEK_SET) < 0) {
(void) fprintf(stderr,
gettext("Partition %s: "), pn);
perror("");
return (PART_NOT_FOUND);
}
return (PART_FOUND);
}
(void) fprintf(stderr, gettext("No such logical drive\n"));
return (PART_NOT_FOUND);
}
/*
* seek_nofdisk
*
* User is asking us to trust them that they know best.
* We basically won't do much seeking here, the only seeking we'll do
* is if the 'hidden' parameter was given.
*/
static
int
seek_nofdisk(int fd, bpb_t *wbpb, off64_t *seekto)
{
if (TotSize > 0xffff)
wbpb->bpb.sectors_in_volume = 0;
else
wbpb->bpb.sectors_in_volume = (short)TotSize;
wbpb->bpb.sectors_in_logical_volume = TotSize;
*seekto = RelOffset * BPSEC;
wbpb->bpb.hidden_sectors = RelOffset;
wbpb->sunbpb.bs_offset_high = RelOffset >> 16;
wbpb->sunbpb.bs_offset_low = RelOffset & 0xFFFF;
if (Verbose)
(void) printf(gettext("Requested offset: Sector %x.\n"),
*seekto/BPSEC);
if (lseek64(fd, *seekto, SEEK_SET) < 0) {
(void) fprintf(stderr,
gettext("User specified start sector %d"), RelOffset);
perror("");
return (PART_NOT_FOUND);
}
return (PART_FOUND);
}
/*
* set_fat_string
*
* Fill in the type string of the FAT
*/
static
void
set_fat_string(bpb_t *wbpb, int fatsize)
{
if (fatsize == 12) {
(void) strncpy((char *)wbpb->ebpb.type, FAT12_TYPE_STRING,
strlen(FAT12_TYPE_STRING));
} else if (fatsize == 16) {
(void) strncpy((char *)wbpb->ebpb.type, FAT16_TYPE_STRING,
strlen(FAT16_TYPE_STRING));
} else {
(void) strncpy((char *)wbpb->ebpb.type, FAT32_TYPE_STRING,
strlen(FAT32_TYPE_STRING));
}
}
/*
* prepare_image_file
*
* Open the file that will hold the image (as opposed to the image
* being written to the boot sector of an actual disk).
*/
static
int
prepare_image_file(char *fn, bpb_t *wbpb)
{
int fd;
char zerobyte = '\0';
if ((fd = open(fn, O_RDWR | O_CREAT | O_EXCL, 0666)) < 0) {
perror(fn);
exit(2);
}
if (Imagesize == 5) {
/* Disk image of a 1.2M floppy */
wbpb->bpb.sectors_in_volume = 2 * 80 * 15;
wbpb->bpb.sectors_in_logical_volume = 2 * 80 * 15;
wbpb->bpb.sectors_per_track = 15;
wbpb->bpb.heads = 2;
wbpb->bpb.media = 0xF9;
wbpb->bpb.num_root_entries = 224;
wbpb->bpb.sectors_per_cluster = 1;
wbpb->bpb.sectors_per_fat = 7;
} else {
/* Disk image of a 1.44M floppy */
wbpb->bpb.sectors_in_volume = 2 * 80 * 18;
wbpb->bpb.sectors_in_logical_volume = 2 * 80 * 18;
wbpb->bpb.sectors_per_track = 18;
wbpb->bpb.heads = 2;
wbpb->bpb.media = 0xF0;
wbpb->bpb.num_root_entries = 224;
wbpb->bpb.sectors_per_cluster = 1;
wbpb->bpb.sectors_per_fat = 9;
}
/*
* Make a holey file, with length the exact
* size of the floppy image.
*/
if (lseek(fd, (wbpb->bpb.sectors_in_volume * BPSEC)-1, SEEK_SET) < 0) {
(void) close(fd);
perror(fn);
exit(2);
}
if (write(fd, &zerobyte, 1) != 1) {
(void) close(fd);
perror(fn);
exit(2);
}
if (lseek(fd, 0, SEEK_SET) < 0) {
(void) close(fd);
perror(fn);
exit(2);
}
Fatentsize = 12; /* Size of fat entry in bits */
set_fat_string(wbpb, Fatentsize);
wbpb->ebpb.phys_drive_num = 0;
wbpb->sunbpb.bs_offset_high = 0;
wbpb->sunbpb.bs_offset_low = 0;
return (fd);
}
/*
* partn_lecture
*
* Give a brief sermon on dev_name user should pass to
* the program from the command line.
*
*/
static
void
partn_lecture(char *dn)
{
(void) fprintf(stderr,
gettext("\nDevice %s was assumed to be a diskette.\n"
"A diskette specific operation failed on this device.\n"
"If the device is a hard disk, provide the name of "
"the full physical disk,\n"
"and qualify that name with a logical drive specifier.\n\n"
"Hint: the device is usually something similar to\n\n"
"/dev/rdsk/c0d0p0 or /dev/rdsk/c0t0d0p0 (x86)\n"
"/dev/rdsk/c0t5d0s2 (sparc)\n\n"
"The drive specifier is appended to the device name."
" For example:\n\n"
"/dev/rdsk/c0t5d0s2:c or /dev/rdsk/c0d0p0:boot\n\n"), dn);
}
static
void
warn_funky_floppy(void)
{
(void) fprintf(stderr,
gettext("Use the 'nofdisk' option to create file systems\n"
"on non-standard floppies.\n\n"));
exit(4);
}
static
void
warn_funky_fatsize(void)
{
(void) fprintf(stderr,
gettext("Non-standard FAT size requested for floppy.\n"
"The 'nofdisk' option must be used to\n"
"override the 12 bit floppy default.\n\n"));
exit(4);
}
static
void
floppy_bpb_fillin(bpb_t *wbpb, int diam, int hds, int spt)
{
switch (diam) {
case 3:
switch (hds) {
case 2:
switch (spt) {
case 9:
wbpb->bpb.media = 0xF9;
wbpb->bpb.num_root_entries = 112;
wbpb->bpb.sectors_per_cluster = 2;
wbpb->bpb.sectors_per_fat = 3;
break;
case 18:
wbpb->bpb.media = 0xF0;
wbpb->bpb.num_root_entries = 224;
wbpb->bpb.sectors_per_cluster = 1;
wbpb->bpb.sectors_per_fat = 9;
break;
case 36:
wbpb->bpb.media = 0xF0;
wbpb->bpb.num_root_entries = 240;
wbpb->bpb.sectors_per_cluster = 2;
wbpb->bpb.sectors_per_fat = 9;
break;
default:
(void) fprintf(stderr,
gettext("Unknown diskette parameters! "
"3.5'' diskette with %d heads "
"and %d sectors/track.\n"), hds, spt);
warn_funky_floppy();
}
break;
case 1:
default:
(void) fprintf(stderr,
gettext("Unknown diskette parameters! "
"3.5'' diskette with %d heads "), hds);
warn_funky_floppy();
}
break;
case 5:
switch (hds) {
case 2:
switch (spt) {
case 15:
wbpb->bpb.media = 0xF9;
wbpb->bpb.num_root_entries = 224;
wbpb->bpb.sectors_per_cluster = 1;
wbpb->bpb.sectors_per_fat = 7;
break;
case 9:
wbpb->bpb.media = 0xFD;
wbpb->bpb.num_root_entries = 112;
wbpb->bpb.sectors_per_cluster = 2;
wbpb->bpb.sectors_per_fat = 2;
break;
case 8:
wbpb->bpb.media = 0xFF;
wbpb->bpb.num_root_entries = 112;
wbpb->bpb.sectors_per_cluster = 1;
wbpb->bpb.sectors_per_fat = 2;
break;
default:
(void) fprintf(stderr,
gettext("Unknown diskette parameters! "
"5.25'' diskette with %d heads "
"and %d sectors/track.\n"), hds, spt);
warn_funky_floppy();
}
break;
case 1:
switch (spt) {
case 9:
wbpb->bpb.media = 0xFC;
wbpb->bpb.num_root_entries = 64;
wbpb->bpb.sectors_per_cluster = 1;
wbpb->bpb.sectors_per_fat = 2;
break;
case 8:
wbpb->bpb.media = 0xFE;
wbpb->bpb.num_root_entries = 64;
wbpb->bpb.sectors_per_cluster = 1;
wbpb->bpb.sectors_per_fat = 1;
break;
default:
(void) fprintf(stderr,
gettext("Unknown diskette parameters! "
"5.25'' diskette with %d heads "
"and %d sectors/track.\n"), hds, spt);
warn_funky_floppy();
}
break;
default:
(void) fprintf(stderr,
gettext("Unknown diskette parameters! "
"5.25'' diskette with %d heads."), hds);
warn_funky_floppy();
}
break;
default:
(void) fprintf(stderr,
gettext("\nUnknown diskette type. Only know about "
"5.25'' and 3.5'' diskettes.\n"));
warn_funky_floppy();
}
}
/*
* lookup_floppy
*
* Look up a media descriptor byte and other crucial BPB values
* based on floppy characteristics.
*/
static
void
lookup_floppy(struct fd_char *fdchar, bpb_t *wbpb)
{
ulong_t tsize;
ulong_t cyls, spt, hds, diam;
cyls = fdchar->fdc_ncyl;
diam = fdchar->fdc_medium;
spt = fdchar->fdc_secptrack;
hds = fdchar->fdc_nhead;
tsize = cyls * hds * spt;
if (GetFsParams)
TotSize = tsize;
if (GetSize) {
wbpb->bpb.sectors_in_logical_volume = tsize;
} else {
wbpb->bpb.sectors_in_logical_volume =
warn_mismatch(
gettext("length of partition (in sectors)"),
gettext("FDIOGCHAR call"), tsize, TotSize);
}
wbpb->bpb.sectors_in_volume =
(short)wbpb->bpb.sectors_in_logical_volume;
if (GetSPT) {
wbpb->bpb.sectors_per_track = spt;
} else {
wbpb->bpb.sectors_per_track =
warn_mismatch(
gettext("sectors per track"),
gettext("FDIOGCHAR call"), spt, SecPerTrk);
spt = wbpb->bpb.sectors_per_track;
}
if (GetTPC) {
wbpb->bpb.heads = hds;
} else {
wbpb->bpb.heads =
warn_mismatch(
gettext("number of heads"),
gettext("FDIOGCHAR call"), hds, TrkPerCyl);
hds = wbpb->bpb.heads;
}
Fatentsize = 12; /* Size of fat entry in bits */
if (!GetBPF && BitsPerFAT != Fatentsize) {
warn_funky_fatsize();
}
set_fat_string(wbpb, Fatentsize);
wbpb->ebpb.phys_drive_num = 0;
wbpb->bpb.hidden_sectors = 0;
wbpb->sunbpb.bs_offset_high = 0;
wbpb->sunbpb.bs_offset_low = 0;
floppy_bpb_fillin(wbpb, diam, hds, spt);
}
/*
* compute_cluster_size
*
* Compute an acceptable sectors/cluster value.
*
* Based on values from the Hardware White Paper
* from Microsoft.
* "Microsoft Extensible Firmware Initiative
* FAT32 File System Specification
* FAT: General Overview of On-Disk Format"
*
* Version 1.03, December 6, 2000
*
*/
static
void
compute_cluster_size(bpb_t *wbpb)
{
ulong_t volsize;
ulong_t spc;
ulong_t rds, tmpval1, tmpval2;
ulong_t fatsz;
int newfat = 16;
#define FAT12_MAX_CLUSTERS 0x0FF4
#define FAT16_MAX_CLUSTERS 0xFFF4
#define FAT32_MAX_CLUSTERS 0x0FFFFFF0
#define FAT32_SUGGESTED_NCLUST 0x400000
/* compute volume size in sectors. */
volsize = wbpb->bpb.sectors_in_volume ? wbpb->bpb.sectors_in_volume :
wbpb->bpb.sectors_in_logical_volume;
volsize -= wbpb->bpb.resv_sectors;
if (GetSPC) {
/*
* User indicated what sort of FAT to create,
* make sure it is valid with the given size
* and compute an SPC value.
*/
if (!MakeFAT32) { /* FAT16 */
/* volsize is in sectors */
if (volsize < FAT12_MAX_CLUSTERS) {
(void) fprintf(stderr,
gettext("Requested size is too "
"small for FAT16.\n"));
exit(4);
}
/* SPC must be a power of 2 */
for (spc = 1; spc <= 64; spc = spc * 2) {
if (volsize < spc * FAT16_MAX_CLUSTERS)
break;
}
if (volsize > (spc * FAT16_MAX_CLUSTERS)) {
(void) fprintf(stderr,
gettext("Requested size is too "
"large for FAT16.\n"));
exit(4);
}
} else { /* FAT32 */
/* volsize is in sectors */
if (volsize < FAT16_MAX_CLUSTERS) {
(void) fprintf(stderr,
gettext("Requested size is too "
"small for FAT32.\n"));
exit(4);
}
/* SPC must be a power of 2 */
for (spc = 1; spc <= 64; spc = spc * 2) {
if (volsize < (spc * FAT32_SUGGESTED_NCLUST))
break;
}
if (volsize > (spc * FAT32_MAX_CLUSTERS)) {
(void) fprintf(stderr,
gettext("Requested size is too "
"large for FAT32.\n"));
exit(4);
}
}
} else {
/*
* User gave the SPC as an explicit option,
* make sure it will work with the requested
* volume size.
*/
int nclust;
spc = SecPerClust;
nclust = volsize / spc;
if (nclust <= FAT16_MAX_CLUSTERS && MakeFAT32) {
(void) fprintf(stderr, gettext("Requested size is too "
"small for FAT32.\n"));
exit(4);
}
if (!MakeFAT32) {
/* Determine if FAT12 or FAT16 */
if (nclust < FAT12_MAX_CLUSTERS)
newfat = 12;
else if (nclust < FAT16_MAX_CLUSTERS)
newfat = 16;
else {
(void) fprintf(stderr,
gettext("Requested size is too "
"small for FAT32.\n"));
exit(4);
}
}
}
/*
* RootDirSectors = ((BPB_RootEntCnt * 32) +
* (BPB_BytsPerSec 1)) / BPB_BytsPerSec;
*/
rds = ((wbpb->bpb.num_root_entries * 32) +
(wbpb->bpb.bytes_sector - 1)) / wbpb->bpb.bytes_sector;
if (GetBPF) {
if (MakeFAT32)
Fatentsize = 32;
else
Fatentsize = newfat;
} else {
Fatentsize = BitsPerFAT;
if (Fatentsize == 12 &&
(volsize - rds) >= DOS_F12MAXC * spc) {
/*
* If we don't have an input TTY, or we aren't
* really doing anything, then don't ask
* questions. Assume a yes answer to any
* questions we would ask.
*/
if (Notreally || !isatty(fileno(stdin))) {
(void) printf(
gettext("Volume too large for 12 bit FAT,"
" increasing to 16 bit FAT size.\n"));
(void) fflush(stdout);
Fatentsize = 16;
} else {
(void) printf(
gettext("Volume too large for a 12 bit FAT.\n"
"Increase to 16 bit FAT "
"and continue (y/n)? "));
(void) fflush(stdout);
if (yes())
Fatentsize = 16;
else
exit(5);
}
}
}
wbpb->bpb.sectors_per_cluster = spc;
if (!GetFsParams && FdiskFATsize < 0) {
(void) printf(
gettext("Cannot verify chosen/computed FAT "
"entry size (%d bits) with FDISK table.\n"
"FDISK table has an unknown file system "
"type for this device. Giving up...\n"),
Fatentsize, Fatentsize);
exit(6);
} else if (!GetFsParams && FdiskFATsize && FdiskFATsize != Fatentsize) {
(void) printf(
gettext("Chosen/computed FAT entry size (%d bits) "
"does not match FDISK table (%d bits).\n"),
Fatentsize, FdiskFATsize);
(void) printf(
gettext("Use -o fat=%d to build a FAT "
"that matches the FDISK entry.\n"), FdiskFATsize);
exit(6);
}
set_fat_string(wbpb, Fatentsize);
/*
* Compure the FAT sizes according to algorithm from Microsoft:
*
* RootDirSectors = ((BPB_RootEntCnt * 32) +
* (BPB_BytsPerSec 1)) / BPB_BytsPerSec;
* TmpVal1 = DskSize - (BPB_ResvdSecCnt + RootDirSectors);
* TmpVal2 = (256 * BPB_SecPerClus) + BPB_NumFATs;
* If (FATType == FAT32)
* TmpVal2 = TmpVal2 / 2;
* FATSz = (TMPVal1 + (TmpVal2 1)) / TmpVal2;
* If (FATType == FAT32) {
* BPB_FATSz16 = 0;
* BPB_FATSz32 = FATSz;
* } else {
* BPB_FATSz16 = LOWORD(FATSz);
* // there is no BPB_FATSz32 in a FAT16 BPB
* }
*/
tmpval1 = volsize - (wbpb->bpb.resv_sectors + rds);
tmpval2 = (256 * wbpb->bpb.sectors_per_cluster) + wbpb->bpb.num_fats;
if (Fatentsize == 32)
tmpval2 = tmpval2 / 2;
fatsz = (tmpval1 + (tmpval2 - 1)) / tmpval2;
/* Compute a sector/fat figure */
switch (Fatentsize) {
case 32:
wbpb->bpb.sectors_per_fat = 0;
wbpb->bpb32.big_sectors_per_fat = fatsz;
if (Verbose)
(void) printf("compute_cluster_size: Sectors per "
"FAT32 = %d\n", wbpb->bpb32.big_sectors_per_fat);
break;
case 12:
default: /* 16 bit FAT */
wbpb->bpb.sectors_per_fat = (ushort_t)(fatsz & 0x0000FFFF);
if (Verbose)
(void) printf("compute_cluster_size: Sectors per "
"FAT16 = %d\n", wbpb->bpb.sectors_per_fat);
break;
}
}
static
void
find_fixed_details(int fd, bpb_t *wbpb)
{
struct dk_geom dginfo;
/*
* Look up the last remaining bits of info we need
* that is specific to the hard drive using a disk ioctl.
*/
if (GetSPT || GetTPC) {
if (ioctl(fd, DKIOCG_VIRTGEOM, &dginfo) == -1 &&
ioctl(fd, DKIOCG_PHYGEOM, &dginfo) == -1 &&
ioctl(fd, DKIOCGGEOM, &dginfo) == -1) {
(void) close(fd);
perror(
gettext("Drive geometry lookup (need "
"tracks/cylinder and/or sectors/track"));
exit(2);
}
}
wbpb->bpb.heads = (GetTPC ? dginfo.dkg_nhead : TrkPerCyl);
wbpb->bpb.sectors_per_track = (GetSPT ? dginfo.dkg_nsect : SecPerTrk);
if (Verbose) {
if (GetTPC) {
(void) printf(
gettext("DKIOCG determined number of heads = %d\n"),
dginfo.dkg_nhead);
}
if (GetSPT) {
(void) printf(
gettext("DKIOCG determined sectors per track"
" = %d\n"), dginfo.dkg_nsect);
}
}
/*
* XXX - MAY need an additional flag (or flags) to set media
* and physical drive number fields. That in the case of weird
* floppies that have to go through 'nofdisk' route for formatting.
*/
wbpb->bpb.media = 0xF8;
if (MakeFAT32)
wbpb->bpb.num_root_entries = 0;
else
wbpb->bpb.num_root_entries = 512;
wbpb->ebpb.phys_drive_num = 0x80;
compute_cluster_size(wbpb);
}
static
char *
stat_actual_disk(char *diskname, struct stat *info, char **suffix)
{
char *actualdisk;
if (stat(diskname, info)) {
/*
* Device named on command line doesn't exist. That
* probably means there is a partition-specifying
* suffix attached to the actual disk name.
*/
actualdisk = strtok(strdup(diskname), ":");
if (*suffix = strchr(diskname, ':'))
(*suffix)++;
if (stat(actualdisk, info)) {
perror(actualdisk);
exit(2);
}
} else {
actualdisk = strdup(diskname);
}
return (actualdisk);
}
static
void
compute_file_area_size(bpb_t *wbpb)
{
int FATSz;
int TotSec;
int DataSec;
int RootDirSectors =
((wbpb->bpb.num_root_entries * 32) + (wbpb->bpb.bytes_sector - 1)) /
wbpb->bpb.bytes_sector;
if (wbpb->bpb.sectors_per_fat) {
/*
* Good old FAT12 or FAT16
*/
FATSz = wbpb->bpb.sectors_per_fat;
TotSec = wbpb->bpb.sectors_in_volume;
} else {
/*
* FAT32
*/
FATSz = wbpb->bpb32.big_sectors_per_fat;
TotSec = wbpb->bpb.sectors_in_logical_volume;
}
DataSec = TotSec -
(wbpb->bpb.resv_sectors + (wbpb->bpb.num_fats * FATSz) +
RootDirSectors);
/*
* Now change sectors to clusters
*/
TotalClusters = DataSec / wbpb->bpb.sectors_per_cluster;
if (Verbose)
(void) printf(gettext("Disk has a file area of %d "
"allocation units,\neach with %d sectors = %d "
"bytes.\n"), TotalClusters, wbpb->bpb.sectors_per_cluster,
TotalClusters * wbpb->bpb.sectors_per_cluster * BPSEC);
}
#ifndef i386
/*
* swap_pack_{bpb,bpb32,sebpb}cpy
*
* If not on an x86 we assume the structures making up the bpb
* were not packed and that longs and shorts need to be byte swapped
* (we've kept everything in host order up until now). A new architecture
* might not need to swap or might not need to pack, in which case
* new routines will have to be written. Of course if an architecture
* supports both packing and little-endian host order, it can follow the
* same path as the x86 code.
*/
static
void
swap_pack_bpbcpy(struct _boot_sector *bsp, bpb_t *wbpb)
{
uchar_t *fillp;
fillp = (uchar_t *)&(bsp->bs_filler[ORIG_BPB_START_INDEX]);
store_16_bits(&fillp, wbpb->bpb.bytes_sector);
*fillp++ = wbpb->bpb.sectors_per_cluster;
store_16_bits(&fillp, wbpb->bpb.resv_sectors);
*fillp++ = wbpb->bpb.num_fats;
store_16_bits(&fillp, wbpb->bpb.num_root_entries);
store_16_bits(&fillp, wbpb->bpb.sectors_in_volume);
*fillp++ = wbpb->bpb.media;
store_16_bits(&fillp, wbpb->bpb.sectors_per_fat);
store_16_bits(&fillp, wbpb->bpb.sectors_per_track);
store_16_bits(&fillp, wbpb->bpb.heads);
store_32_bits(&fillp, wbpb->bpb.hidden_sectors);
store_32_bits(&fillp, wbpb->bpb.sectors_in_logical_volume);
*fillp++ = wbpb->ebpb.phys_drive_num;
*fillp++ = wbpb->ebpb.reserved;
*fillp++ = wbpb->ebpb.ext_signature;
store_32_bits(&fillp, wbpb->ebpb.volume_id);
(void) strncpy((char *)fillp, (char *)wbpb->ebpb.volume_label, 11);
fillp += 11;
(void) strncpy((char *)fillp, (char *)wbpb->ebpb.type, 8);
}
static
void
swap_pack_bpb32cpy(struct _boot_sector32 *bsp, bpb_t *wbpb)
{
uchar_t *fillp;
int r;
fillp = (uchar_t *)&(bsp->bs_filler[ORIG_BPB_START_INDEX]);
store_16_bits(&fillp, wbpb->bpb.bytes_sector);
*fillp++ = wbpb->bpb.sectors_per_cluster;
store_16_bits(&fillp, wbpb->bpb.resv_sectors);
*fillp++ = wbpb->bpb.num_fats;
store_16_bits(&fillp, wbpb->bpb.num_root_entries);
store_16_bits(&fillp, wbpb->bpb.sectors_in_volume);
*fillp++ = wbpb->bpb.media;
store_16_bits(&fillp, wbpb->bpb.sectors_per_fat);
store_16_bits(&fillp, wbpb->bpb.sectors_per_track);
store_16_bits(&fillp, wbpb->bpb.heads);
store_32_bits(&fillp, wbpb->bpb.hidden_sectors);
store_32_bits(&fillp, wbpb->bpb.sectors_in_logical_volume);
store_32_bits(&fillp, wbpb->bpb32.big_sectors_per_fat);
store_16_bits(&fillp, wbpb->bpb32.ext_flags);
*fillp++ = wbpb->bpb32.fs_vers_lo;
*fillp++ = wbpb->bpb32.fs_vers_hi;
store_32_bits(&fillp, wbpb->bpb32.root_dir_clust);
store_16_bits(&fillp, wbpb->bpb32.fsinfosec);
store_16_bits(&fillp, wbpb->bpb32.backupboot);
for (r = 0; r < 6; r++)
store_16_bits(&fillp, wbpb->bpb32.reserved[r]);
*fillp++ = wbpb->ebpb.phys_drive_num;
*fillp++ = wbpb->ebpb.reserved;
*fillp++ = wbpb->ebpb.ext_signature;
store_32_bits(&fillp, wbpb->ebpb.volume_id);
(void) strncpy((char *)fillp, (char *)wbpb->ebpb.volume_label, 11);
fillp += 11;
(void) strncpy((char *)fillp, (char *)wbpb->ebpb.type, 8);
}
static
void
swap_pack_sebpbcpy(struct _boot_sector *bsp, bpb_t *wbpb)
{
uchar_t *fillp;
fillp = bsp->bs_sun_bpb;
store_16_bits(&fillp, wbpb->sunbpb.bs_offset_high);
store_16_bits(&fillp, wbpb->sunbpb.bs_offset_low);
}
static
void
swap_pack_grabbpb(bpb_t *wbpb, struct _boot_sector *bsp)
{
uchar_t *grabp;
grabp = (uchar_t *)&(bsp->bs_filler[ORIG_BPB_START_INDEX]);
((uchar_t *)&(wbpb->bpb.bytes_sector))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb.bytes_sector))[0] = *grabp++;
wbpb->bpb.sectors_per_cluster = *grabp++;
((uchar_t *)&(wbpb->bpb.resv_sectors))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb.resv_sectors))[0] = *grabp++;
wbpb->bpb.num_fats = *grabp++;
((uchar_t *)&(wbpb->bpb.num_root_entries))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb.num_root_entries))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb.sectors_in_volume))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb.sectors_in_volume))[0] = *grabp++;
wbpb->bpb.media = *grabp++;
((uchar_t *)&(wbpb->bpb.sectors_per_fat))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb.sectors_per_fat))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb.sectors_per_track))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb.sectors_per_track))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb.heads))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb.heads))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb.hidden_sectors))[3] = *grabp++;
((uchar_t *)&(wbpb->bpb.hidden_sectors))[2] = *grabp++;
((uchar_t *)&(wbpb->bpb.hidden_sectors))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb.hidden_sectors))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb.sectors_in_logical_volume))[3] = *grabp++;
((uchar_t *)&(wbpb->bpb.sectors_in_logical_volume))[2] = *grabp++;
((uchar_t *)&(wbpb->bpb.sectors_in_logical_volume))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb.sectors_in_logical_volume))[0] = *grabp++;
wbpb->ebpb.phys_drive_num = *grabp++;
wbpb->ebpb.reserved = *grabp++;
wbpb->ebpb.ext_signature = *grabp++;
((uchar_t *)&(wbpb->ebpb.volume_id))[3] = *grabp++;
((uchar_t *)&(wbpb->ebpb.volume_id))[2] = *grabp++;
((uchar_t *)&(wbpb->ebpb.volume_id))[1] = *grabp++;
((uchar_t *)&(wbpb->ebpb.volume_id))[0] = *grabp++;
(void) strncpy((char *)wbpb->ebpb.volume_label, (char *)grabp, 11);
grabp += 11;
(void) strncpy((char *)wbpb->ebpb.type, (char *)grabp, 8);
}
static
void
swap_pack_grabsebpb(bpb_t *wbpb, struct _boot_sector *bsp)
{
uchar_t *grabp;
grabp = bsp->bs_sun_bpb;
((uchar_t *)&(wbpb->sunbpb.bs_offset_high))[1] = *grabp++;
((uchar_t *)&(wbpb->sunbpb.bs_offset_high))[0] = *grabp++;
((uchar_t *)&(wbpb->sunbpb.bs_offset_low))[1] = *grabp++;
((uchar_t *)&(wbpb->sunbpb.bs_offset_low))[0] = *grabp++;
}
static
void
swap_pack_grab32bpb(bpb_t *wbpb, struct _boot_sector *bsp)
{
uchar_t *grabp;
grabp = (uchar_t *)&(bsp->bs_filler[BPB_32_START_INDEX]);
((uchar_t *)&(wbpb->bpb32.big_sectors_per_fat))[3] = *grabp++;
((uchar_t *)&(wbpb->bpb32.big_sectors_per_fat))[2] = *grabp++;
((uchar_t *)&(wbpb->bpb32.big_sectors_per_fat))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.big_sectors_per_fat))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb32.ext_flags))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.ext_flags))[0] = *grabp++;
wbpb->bpb32.fs_vers_lo = *grabp++;
wbpb->bpb32.fs_vers_hi = *grabp++;
((uchar_t *)&(wbpb->bpb32.root_dir_clust))[3] = *grabp++;
((uchar_t *)&(wbpb->bpb32.root_dir_clust))[2] = *grabp++;
((uchar_t *)&(wbpb->bpb32.root_dir_clust))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.root_dir_clust))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb32.fsinfosec))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.fsinfosec))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb32.backupboot))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.backupboot))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[0]))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[0]))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[1]))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[1]))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[2]))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[2]))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[3]))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[3]))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[4]))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[4]))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[5]))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[5]))[0] = *grabp++;
}
#endif /* ! i386 */
static
void
dashm_bail(int fd)
{
(void) fprintf(stderr,
gettext("This media does not appear to be "
"formatted with a FAT file system.\n"));
(void) close(fd);
exit(6);
}
/*
* read_existing_bpb
*
* Grab the first sector, which we think is a bios parameter block.
* If it looks bad, bail. Otherwise fill in the parameter struct
* fields that matter.
*/
static
void
read_existing_bpb(int fd, bpb_t *wbpb)
{
boot_sector_t ubpb;
if (read(fd, ubpb.buf, BPSEC) < BPSEC) {
perror(gettext("Read BIOS parameter block "
"from previously formatted media"));
(void) close(fd);
exit(6);
}
if (ltohs(ubpb.mb.signature) != BOOTSECSIG) {
dashm_bail(fd);
}
#ifdef i386
(void) memcpy(&(wbpb->bpb), &(ubpb.bs.bs_front.bs_bpb),
sizeof (wbpb->bpb));
(void) memcpy(&(wbpb->ebpb), &(ubpb.bs.bs_ebpb), sizeof (wbpb->ebpb));
#else
swap_pack_grabbpb(wbpb, &(ubpb.bs));
#endif
if (SunBPBfields) {
#ifdef i386
(void) memcpy(&(wbpb->sunbpb), &(ubpb.bs.bs_sebpb),
sizeof (wbpb->sunbpb));
#else
swap_pack_grabsebpb(wbpb, &(ubpb.bs));
#endif
}
if (wbpb->bpb.bytes_sector != BPSEC) {
(void) fprintf(stderr,
gettext("Bogus bytes per sector value.\n"));
if (!(ISP2(wbpb->bpb.bytes_sector) &&
IN_RANGE(wbpb->bpb.bytes_sector, 1, BPSEC * 8))) {
(void) fprintf(stderr,
gettext("The device name may be missing a "
"logical drive specifier.\n"));
(void) close(fd);
exit(6);
} else {
(void) fprintf(stderr,
gettext("Do not know how to build FATs with a\n"
"non-standard sector size. Standard "
"size is %d bytes,\nyour sector size "
"is %d bytes.\n"), BPSEC,
wbpb->bpb.bytes_sector);
(void) close(fd);
exit(6);
}
}
if (!(ISP2(wbpb->bpb.sectors_per_cluster) &&
IN_RANGE(wbpb->bpb.sectors_per_cluster, 1, 128))) {
(void) fprintf(stderr,
gettext("Bogus sectors per cluster value.\n"));
(void) fprintf(stderr,
gettext("The device name may be missing a "
"logical drive specifier.\n"));
(void) close(fd);
exit(6);
}
if (wbpb->bpb.sectors_per_fat == 0) {
#ifdef i386
(void) memcpy(&(wbpb->bpb32), &(ubpb.bs32.bs_bpb32),
sizeof (wbpb->bpb32));
#else
swap_pack_grab32bpb(wbpb, &(ubpb.bs));
#endif
compute_file_area_size(wbpb);
if ((wbpb->bpb32.big_sectors_per_fat * BPSEC / 4) >=
TotalClusters) {
MakeFAT32 = 1;
} else {
dashm_bail(fd);
}
} else {
compute_file_area_size(wbpb);
}
}
/*
* compare_existing_with_computed
*
* We use this function when we the user specifies the -m option.
* We compute and look up things like we would if they had asked
* us to make the fs, and compare that to what's already layed down
* in the existing fs. If there's a difference we can tell them what
* options to specify in order to reproduce their existing layout.
* Note that they still may not get an exact duplicate, because we
* don't, for example, preserve their existing boot code. We think
* we've got all the fields that matter covered, though.
*
* XXX - We're basically ignoring sbpb at this point. I'm unsure
* if we'll ever care about those fields, in terms of the -m option.
*/
static
void
compare_existing_with_computed(int fd, char *suffix,
bpb_t *wbpb, int *prtsize, int *prtspc, int *prtbpf, int *prtnsect,
int *prtntrk, int *prtfdisk, int *prthidden, int *prtrsrvd, int *dashos)
{
struct dk_geom dginfo;
struct fd_char fdchar;
bpb_t compare;
int fd_ioctl_worked = 0;
int fatents;
/*
* For all non-floppy cases we expect to find a 16-bit FAT
*/
int expectfatsize = 16;
compare = *wbpb;
if (!suffix) {
if (ioctl(fd, FDIOGCHAR, &fdchar) != -1) {
expectfatsize = 12;
fd_ioctl_worked++;
}
}
if (fd_ioctl_worked) {
#ifdef sparc
fdchar.fdc_medium = 3;
#endif
GetSize = GetSPT = GetSPC = GetTPC = GetBPF = 1;
lookup_floppy(&fdchar, &compare);
if (compare.bpb.heads != wbpb->bpb.heads) {
(*prtntrk)++;
(*dashos)++;
}
if (compare.bpb.sectors_per_track !=
wbpb->bpb.sectors_per_track) {
(*prtnsect)++;
(*dashos)++;
}
} else {
int dk_ioctl_worked = 1;
if (!suffix) {
(*prtfdisk)++;
(*prtsize)++;
*dashos += 2;
}
if (ioctl(fd, DKIOCG_VIRTGEOM, &dginfo) == -1 &&
ioctl(fd, DKIOCG_PHYGEOM, &dginfo) == -1 &&
ioctl(fd, DKIOCGGEOM, &dginfo) == -1) {
*prtnsect = *prtntrk = 1;
*dashos += 2;
dk_ioctl_worked = 0;
}
if (dk_ioctl_worked) {
if (dginfo.dkg_nhead != wbpb->bpb.heads) {
(*prtntrk)++;
(*dashos)++;
}
if (dginfo.dkg_nsect !=
wbpb->bpb.sectors_per_track) {
(*prtnsect)++;
(*dashos)++;
}
}
GetBPF = GetSPC = 1;
compute_cluster_size(&compare);
}
if (!*prtfdisk && TotSize != wbpb->bpb.sectors_in_volume &&
TotSize != wbpb->bpb.sectors_in_logical_volume) {
(*dashos)++;
(*prtsize)++;
}
if (compare.bpb.sectors_per_cluster != wbpb->bpb.sectors_per_cluster) {
(*dashos)++;
(*prtspc)++;
}
if (compare.bpb.hidden_sectors != wbpb->bpb.hidden_sectors) {
(*dashos)++;
(*prthidden)++;
}
if (compare.bpb.resv_sectors != wbpb->bpb.resv_sectors) {
(*dashos)++;
(*prtrsrvd)++;
}
/*
* Compute approximate Fatentsize. It's approximate because the
* size of the FAT may not be exactly a multiple of the number of
* clusters. It should be close, though.
*/
if (MakeFAT32) {
Fatentsize = 32;
(*dashos)++;
(*prtbpf)++;
} else {
fatents = wbpb->bpb.sectors_per_fat * BPSEC * 2 / 3;
if (fatents >= TotalClusters && wbpb->ebpb.type[4] == '2')
Fatentsize = 12;
else
Fatentsize = 16;
if (Fatentsize != expectfatsize) {
(*dashos)++;
(*prtbpf)++;
}
}
}
static
void
print_reproducing_command(int fd, char *actualdisk, char *suffix, bpb_t *wbpb)
{
int needcomma = 0;
int prthidden = 0;
int prtrsrvd = 0;
int prtfdisk = 0;
int prtnsect = 0;
int prtntrk = 0;
int prtsize = 0;
int prtbpf = 0;
int prtspc = 0;
int dashos = 0;
int ll, i;
compare_existing_with_computed(fd, suffix, wbpb,
&prtsize, &prtspc, &prtbpf, &prtnsect, &prtntrk,
&prtfdisk, &prthidden, &prtrsrvd, &dashos);
/*
* Print out the command line they can use to reproduce the
* file system.
*/
(void) printf("mkfs -F pcfs");
ll = min(11, (int)strlen((char *)wbpb->ebpb.volume_label));
/*
* First, eliminate trailing spaces. Now compare the name against
* our default label. If there's a match we don't need to print
* any label info.
*/
i = ll;
while (wbpb->ebpb.volume_label[--i] == ' ')
;
ll = i;
if (ll == strlen(DEFAULT_LABEL) - 1) {
char cmpbuf[11];
(void) strcpy(cmpbuf, DEFAULT_LABEL);
for (i = ll; i >= 0; i--) {
if (cmpbuf[i] !=
toupper((int)(wbpb->ebpb.volume_label[i]))) {
break;
}
}
if (i < 0)
ll = i;
}
if (ll >= 0) {
(void) printf(" -o ");
(void) printf("b=\"");
for (i = 0; i <= ll; i++) {
(void) printf("%c", wbpb->ebpb.volume_label[i]);
}
(void) printf("\"");
needcomma++;
} else if (dashos) {
(void) printf(" -o ");
}
#define NEXT_DASH_O dashos--; needcomma++; continue
while (dashos) {
if (needcomma) {
(void) printf(",");
needcomma = 0;
}
if (prtfdisk) {
(void) printf("nofdisk");
prtfdisk--;
NEXT_DASH_O;
}
if (prtsize) {
(void) printf("size=%u", wbpb->bpb.sectors_in_volume ?
wbpb->bpb.sectors_in_volume :
wbpb->bpb.sectors_in_logical_volume);
prtsize--;
NEXT_DASH_O;
}
if (prtnsect) {
(void) printf("nsect=%d", wbpb->bpb.sectors_per_track);
prtnsect--;
NEXT_DASH_O;
}
if (prtspc) {
(void) printf("spc=%d", wbpb->bpb.sectors_per_cluster);
prtspc--;
NEXT_DASH_O;
}
if (prtntrk) {
(void) printf("ntrack=%d", wbpb->bpb.heads);
prtntrk--;
NEXT_DASH_O;
}
if (prtbpf) {
(void) printf("fat=%d", Fatentsize);
prtbpf--;
NEXT_DASH_O;
}
if (prthidden) {
(void) printf("hidden=%u", wbpb->bpb.hidden_sectors);
prthidden--;
NEXT_DASH_O;
}
if (prtrsrvd) {
(void) printf("reserve=%d", wbpb->bpb.resv_sectors);
prtrsrvd--;
NEXT_DASH_O;
}
}
(void) printf(" %s%c%c\n", actualdisk,
suffix ? ':' : '\0', suffix ? *suffix : '\0');
}
/*
* open_and_examine
*
* Open the requested 'dev_name'. Seek to point where
* we'd expect to find boot sectors, etc., based on any ':partition'
* attachments to the dev_name.
*
* Examine the fields of any existing boot sector and display best
* approximation of how this fs could be reproduced with this command.
*/
static
int
open_and_examine(char *dn, bpb_t *wbpb)
{
struct stat di;
off64_t ignored;
char *actualdisk = NULL;
char *suffix = NULL;
int fd;
struct dk_minfo dkminfo;
if (Verbose)
(void) printf(gettext("Opening destination device/file.\n"));
actualdisk = stat_actual_disk(dn, &di, &suffix);
/*
* Destination exists, now find more about it.
*/
if (!(S_ISCHR(di.st_mode))) {
(void) fprintf(stderr,
gettext("\n%s: device name must be a "
"character special device.\n"), actualdisk);
exit(2);
} else if ((fd = open(actualdisk, O_RDWR)) < 0) {
perror(actualdisk);
exit(2);
}
/*
* Check the media sector size
*/
if (ioctl(fd, DKIOCGMEDIAINFO, &dkminfo) != -1) {
if (dkminfo.dki_lbsize != 0 &&
ISP2(dkminfo.dki_lbsize / DEV_BSIZE) &&
dkminfo.dki_lbsize != DEV_BSIZE) {
(void) fprintf(stderr,
gettext("The device sector size %u is not "
"supported by pcfs!\n"), dkminfo.dki_lbsize);
(void) close(fd);
exit(1);
}
}
/*
* Find appropriate partition if we were requested to do so.
*/
if (suffix && !(seek_partn(fd, suffix, wbpb, &ignored))) {
(void) close(fd);
exit(2);
}
read_existing_bpb(fd, wbpb);
print_reproducing_command(fd, actualdisk, suffix, wbpb);
return (fd);
}
/*
* open_and_seek
*
* Open the requested 'dev_name'. Seek to point where
* we'll write boot sectors, etc., based on any ':partition'
* attachments to the dev_name.
*
* By the time we are finished here, the entire BPB will be
* filled in, excepting the volume label.
*/
static
int
open_and_seek(char *dn, bpb_t *wbpb, off64_t *seekto)
{
struct fd_char fdchar;
struct dk_geom dg;
struct stat di;
struct dk_minfo dkminfo;
char *actualdisk = NULL;
char *suffix = NULL;
int fd;
if (Verbose)
(void) printf(gettext("Opening destination device/file.\n"));
/*
* We hold these truths to be self evident, all BPBs we create
* will have these values in these fields.
*/
wbpb->bpb.num_fats = 2;
wbpb->bpb.bytes_sector = BPSEC;
/*
* Assign or use supplied numbers for hidden and
* reserved sectors in the file system.
*/
if (GetResrvd)
if (MakeFAT32)
wbpb->bpb.resv_sectors = 32;
else
wbpb->bpb.resv_sectors = 1;
else
wbpb->bpb.resv_sectors = Resrvd;
wbpb->ebpb.ext_signature = 0x29; /* Magic number for modern format */
wbpb->ebpb.volume_id = 0;
if (MakeFAT32)
fill_fat32_bpb(wbpb);
/*
* If all output goes to a simple file, call a routine to setup
* that scenario. Otherwise, try to find the device.
*/
if (Outputtofile)
return (fd = prepare_image_file(dn, wbpb));
actualdisk = stat_actual_disk(dn, &di, &suffix);
/*
* Sanity check. If we've been provided a partition-specifying
* suffix, we shouldn't also have been told to ignore the
* fdisk table.
*/
if (DontUseFdisk && suffix) {
(void) fprintf(stderr,
gettext("Using 'nofdisk' option precludes "
"appending logical drive\nspecifier "
"to the device name.\n"));
exit(2);
}
/*
* Destination exists, now find more about it.
*/
if (!(S_ISCHR(di.st_mode))) {
(void) fprintf(stderr,
gettext("\n%s: device name must indicate a "
"character special device.\n"), actualdisk);
exit(2);
} else if ((fd = open(actualdisk, O_RDWR)) < 0) {
perror(actualdisk);
exit(2);
}
/*
* Check the media sector size
*/
if (ioctl(fd, DKIOCGMEDIAINFO, &dkminfo) != -1) {
if (dkminfo.dki_lbsize != 0 &&
ISP2(dkminfo.dki_lbsize / DEV_BSIZE) &&
dkminfo.dki_lbsize != DEV_BSIZE) {
(void) fprintf(stderr,
gettext("The device sector size %u is not "
"supported by pcfs!\n"), dkminfo.dki_lbsize);
(void) close(fd);
exit(1);
}
}
/*
* Find appropriate partition if we were requested to do so.
*/
if (suffix && !(seek_partn(fd, suffix, wbpb, seekto))) {
(void) close(fd);
exit(2);
}
if (!suffix) {
/*
* We have one of two possibilities. Chances are we have
* a floppy drive. But the user may be trying to format
* some weird drive that we don't know about and is supplying
* all the important values. In that case, they should have set
* the 'nofdisk' flag.
*
* If 'nofdisk' isn't set, do a floppy-specific ioctl to
* get the remainder of our info. If the ioctl fails, we have
* a good idea that they aren't really on a floppy. In that
* case, they should have given us a partition specifier.
*/
if (DontUseFdisk) {
if (!(seek_nofdisk(fd, wbpb, seekto))) {
(void) close(fd);
exit(2);
}
find_fixed_details(fd, wbpb);
} else if (ioctl(fd, FDIOGCHAR, &fdchar) == -1) {
/*
* It is possible that we are trying to use floppy
* specific FDIOGCHAR ioctl on USB floppy. Since sd
* driver, by which USB floppy is handled, doesn't
* support it, we can try to use disk DKIOCGGEOM ioctl
* to retrieve data we need. sd driver itself
* determines floppy disk by number of blocks
* (<=0x1000), then it sets geometry to 80 cylinders,
* 2 heads.
*
* Note that DKIOCGGEOM cannot supply us with type
* of media (e.g. 3.5" or 5.25"). We will set it to
* 3 (3.5") which is most probable value.
*/
if (errno == ENOTTY) {
if (ioctl(fd, DKIOCGGEOM, &dg) != -1 &&
dg.dkg_ncyl == 80 && dg.dkg_nhead == 2) {
fdchar.fdc_ncyl = dg.dkg_ncyl;
fdchar.fdc_medium = 3;
fdchar.fdc_secptrack = dg.dkg_nsect;
fdchar.fdc_nhead = dg.dkg_nhead;
lookup_floppy(&fdchar, wbpb);
} else {
partn_lecture(actualdisk);
(void) close(fd);
exit(2);
}
}
} else {
#ifdef sparc
fdchar.fdc_medium = 3;
#endif
lookup_floppy(&fdchar, wbpb);
}
} else {
find_fixed_details(fd, wbpb);
}
return (fd);
}
/*
* The following is a copy of MS-DOS 4.0 boot block.
* It consists of the BIOS parameter block, and a disk
* bootstrap program.
*
* The BIOS parameter block contains the right values
* for the 3.5" high-density 1.44MB floppy format.
*
* This will be our default boot sector, if the user
* didn't point us at a different one.
*
*/
static
uchar_t DefBootSec[512] = {
0xeb, 0x3c, 0x90, /* 8086 short jump + displacement + NOP */
'M', 'S', 'D', 'O', 'S', '4', '.', '0', /* OEM name & version */
0x00, 0x02, 0x01, 0x01, 0x00,
0x02, 0xe0, 0x00, 0x40, 0x0b,
0xf0, 0x09, 0x00, 0x12, 0x00,
0x02, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
0x29, 0x00, 0x00, 0x00, 0x00,
'N', 'O', 'N', 'A', 'M', 'E', ' ', ' ', ' ', ' ', ' ',
'F', 'A', 'T', '1', '2', ' ', ' ', ' ',
0xfa, 0x33,
0xc0, 0x8e, 0xd0, 0xbc, 0x00, 0x7c, 0x16, 0x07,
0xbb, 0x78, 0x00, 0x36, 0xc5, 0x37, 0x1e, 0x56,
0x16, 0x53, 0xbf, 0x3e, 0x7c, 0xb9, 0x0b, 0x00,
0xfc, 0xf3, 0xa4, 0x06, 0x1f, 0xc6, 0x45, 0xfe,
0x0f, 0x8b, 0x0e, 0x18, 0x7c, 0x88, 0x4d, 0xf9,
0x89, 0x47, 0x02, 0xc7, 0x07, 0x3e, 0x7c, 0xfb,
0xcd, 0x13, 0x72, 0x7c, 0x33, 0xc0, 0x39, 0x06,
0x13, 0x7c, 0x74, 0x08, 0x8b, 0x0e, 0x13, 0x7c,
0x89, 0x0e, 0x20, 0x7c, 0xa0, 0x10, 0x7c, 0xf7,
0x26, 0x16, 0x7c, 0x03, 0x06, 0x1c, 0x7c, 0x13,
0x16, 0x1e, 0x7c, 0x03, 0x06, 0x0e, 0x7c, 0x83,
0xd2, 0x00, 0xa3, 0x50, 0x7c, 0x89, 0x16, 0x52,
0x7c, 0xa3, 0x49, 0x7c, 0x89, 0x16, 0x4b, 0x7c,
0xb8, 0x20, 0x00, 0xf7, 0x26, 0x11, 0x7c, 0x8b,
0x1e, 0x0b, 0x7c, 0x03, 0xc3, 0x48, 0xf7, 0xf3,
0x01, 0x06, 0x49, 0x7c, 0x83, 0x16, 0x4b, 0x7c,
0x00, 0xbb, 0x00, 0x05, 0x8b, 0x16, 0x52, 0x7c,
0xa1, 0x50, 0x7c, 0xe8, 0x87, 0x00, 0x72, 0x20,
0xb0, 0x01, 0xe8, 0xa1, 0x00, 0x72, 0x19, 0x8b,
0xfb, 0xb9, 0x0b, 0x00, 0xbe, 0xdb, 0x7d, 0xf3,
0xa6, 0x75, 0x0d, 0x8d, 0x7f, 0x20, 0xbe, 0xe6,
0x7d, 0xb9, 0x0b, 0x00, 0xf3, 0xa6, 0x74, 0x18,
0xbe, 0x93, 0x7d, 0xe8, 0x51, 0x00, 0x32, 0xe4,
0xcd, 0x16, 0x5e, 0x1f, 0x8f, 0x04, 0x8f, 0x44,
0x02, 0xcd, 0x19, 0x58, 0x58, 0x58, 0xeb, 0xe8,
0xbb, 0x00, 0x07, 0xb9, 0x03, 0x00, 0xa1, 0x49,
0x7c, 0x8b, 0x16, 0x4b, 0x7c, 0x50, 0x52, 0x51,
0xe8, 0x3a, 0x00, 0x72, 0xe6, 0xb0, 0x01, 0xe8,
0x54, 0x00, 0x59, 0x5a, 0x58, 0x72, 0xc9, 0x05,
0x01, 0x00, 0x83, 0xd2, 0x00, 0x03, 0x1e, 0x0b,
0x7c, 0xe2, 0xe2, 0x8a, 0x2e, 0x15, 0x7c, 0x8a,
0x16, 0x24, 0x7c, 0x8b, 0x1e, 0x49, 0x7c, 0xa1,
0x4b, 0x7c, 0xea, 0x00, 0x00, 0x70, 0x00, 0xac,
0x0a, 0xc0, 0x74, 0x29, 0xb4, 0x0e, 0xbb, 0x07,
0x00, 0xcd, 0x10, 0xeb, 0xf2, 0x3b, 0x16, 0x18,
0x7c, 0x73, 0x19, 0xf7, 0x36, 0x18, 0x7c, 0xfe,
0xc2, 0x88, 0x16, 0x4f, 0x7c, 0x33, 0xd2, 0xf7,
0x36, 0x1a, 0x7c, 0x88, 0x16, 0x25, 0x7c, 0xa3,
0x4d, 0x7c, 0xf8, 0xc3, 0xf9, 0xc3, 0xb4, 0x02,
0x8b, 0x16, 0x4d, 0x7c, 0xb1, 0x06, 0xd2, 0xe6,
0x0a, 0x36, 0x4f, 0x7c, 0x8b, 0xca, 0x86, 0xe9,
0x8a, 0x16, 0x24, 0x7c, 0x8a, 0x36, 0x25, 0x7c,
0xcd, 0x13, 0xc3, 0x0d, 0x0a, 0x4e, 0x6f, 0x6e,
0x2d, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x20,
0x64, 0x69, 0x73, 0x6b, 0x20, 0x6f, 0x72, 0x20,
0x64, 0x69, 0x73, 0x6b, 0x20, 0x65, 0x72, 0x72,
0x6f, 0x72, 0x0d, 0x0a, 0x52, 0x65, 0x70, 0x6c,
0x61, 0x63, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20,
0x70, 0x72, 0x65, 0x73, 0x73, 0x20, 0x61, 0x6e,
0x79, 0x20, 0x6b, 0x65, 0x79, 0x20, 0x77, 0x68,
0x65, 0x6e, 0x20, 0x72, 0x65, 0x61, 0x64, 0x79,
0x0d, 0x0a, 0x00, 0x49, 0x4f, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x53, 0x59, 0x53, 0x4d, 0x53,
0x44, 0x4f, 0x53, 0x20, 0x20, 0x20, 0x53, 0x59,
0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa
};
/*
* verify_bootblkfile
*
* We were provided with the name of a file containing the bootblk
* to install. Verify it has a valid boot sector as best we can. Any
* errors and we return a bad file descriptor. Otherwise we fill up the
* provided buffer with the boot sector, return the file
* descriptor for later use and leave the file pointer just
* past the boot sector part of the boot block file.
*/
static
int
verify_bootblkfile(char *fn, boot_sector_t *bs, ulong_t *blkfilesize)
{
struct stat fi;
int bsfd = -1;
if (stat(fn, &fi)) {
perror(fn);
} else if (fi.st_size < BPSEC) {
(void) fprintf(stderr,
gettext("%s: Too short to be a boot sector.\n"), fn);
} else if ((bsfd = open(fn, O_RDONLY)) < 0) {
perror(fn);
} else if (read(bsfd, bs->buf, BPSEC) < BPSEC) {
(void) close(bsfd);
bsfd = -1;
perror(gettext("Boot block read"));
} else {
if ((bs->bs.bs_signature[0] != (BOOTSECSIG & 0xFF) &&
bs->bs.bs_signature[1] != ((BOOTSECSIG >> 8) & 0xFF)) ||
#ifdef i386
(bs->bs.bs_front.bs_jump_code[0] != OPCODE1 &&
bs->bs.bs_front.bs_jump_code[0] != OPCODE2)
#else
(bs->bs.bs_jump_code[0] != OPCODE1 &&
bs->bs.bs_jump_code[0] != OPCODE2)
#endif
/* CSTYLED */
) {
(void) close(bsfd);
bsfd = -1;
(void) fprintf(stderr,
gettext("Boot block (%s) bogus.\n"), fn);
}
*blkfilesize = fi.st_size;
}
return (bsfd);
}
/*
* verify_firstfile
*
* We were provided with the name of a file to be the first file
* installed on the disk. We just need to verify it exists and
* find out how big it is. If it doesn't exist, we print a warning
* message about how the file wasn't found. We don't exit fatally,
* though, rather we return a size of 0 and the FAT will be built
* without installing any first file. They can then presumably
* install the correct first file by hand.
*/
static
int
verify_firstfile(char *fn, ulong_t *filesize)
{
struct stat fi;
int fd = -1;
*filesize = 0;
if (stat(fn, &fi) || (fd = open(fn, O_RDONLY)) < 0) {
perror(fn);
(void) fprintf(stderr,
gettext("Could not access requested file. It will not\n"
"be installed in the new file system.\n"));
} else {
*filesize = fi.st_size;
}
return (fd);
}
/*
* label_volume
*
* Fill in BPB with volume label.
*/
static
void
label_volume(char *lbl, bpb_t *wbpb)
{
int ll, i;
/* Put a volume label into our BPB. */
if (!lbl)
lbl = DEFAULT_LABEL;
ll = min(11, (int)strlen(lbl));
for (i = 0; i < ll; i++) {
wbpb->ebpb.volume_label[i] = toupper(lbl[i]);
}
for (; i < 11; i++) {
wbpb->ebpb.volume_label[i] = ' ';
}
}
static
int
copy_bootblk(char *fn, boot_sector_t *bootsect, ulong_t *bootblksize)
{
int bsfd = -1;
if (Verbose && fn)
(void) printf(gettext("Request to install boot "
"block file %s.\n"), fn);
else if (Verbose)
(void) printf(gettext("Request to install DOS boot block.\n"));
/*
* If they want to install their own boot block, sanity check
* that block.
*/
if (fn) {
bsfd = verify_bootblkfile(fn, bootsect, bootblksize);
if (bsfd < 0) {
exit(3);
}
*bootblksize = roundup(*bootblksize, BPSEC);
} else {
(void) memcpy(bootsect, DefBootSec, BPSEC);
*bootblksize = BPSEC;
}
return (bsfd);
}
/*
* mark_cluster
*
* This routine fills a FAT entry with the value supplied to it as an
* argument. The fatp argument is assumed to be a pointer to the FAT's
* 0th entry. The clustnum is the cluster entry that should be updated.
* The value is the new value for the entry.
*/
static
void
mark_cluster(uchar_t *fatp, pc_cluster32_t clustnum, uint32_t value)
{
uchar_t *ep;
ulong_t idx;
idx = (Fatentsize == 32) ? clustnum * 4 :
(Fatentsize == 16) ? clustnum * 2 : clustnum + clustnum/2;
ep = fatp + idx;
if (Fatentsize == 32) {
store_32_bits(&ep, value);
} else if (Fatentsize == 16) {
store_16_bits(&ep, value);
} else {
if (clustnum & 1) {
*ep = (*ep & 0x0f) | ((value << 4) & 0xf0);
ep++;
*ep = (value >> 4) & 0xff;
} else {
*ep++ = value & 0xff;
*ep = (*ep & 0xf0) | ((value >> 8) & 0x0f);
}
}
}
static
uchar_t *
build_fat(bpb_t *wbpb, struct fat_od_fsi *fsinfop, ulong_t bootblksize,
ulong_t *fatsize, char *ffn, int *fffd, ulong_t *ffsize,
pc_cluster32_t *ffstartclust)
{
pc_cluster32_t nextfree, ci;
uchar_t *fatp;
ushort_t numclust, numsect;
int remclust;
/* Alloc space for a FAT and then null it out. */
if (Verbose) {
(void) printf(gettext("BUILD FAT.\n%d sectors per fat.\n"),
wbpb->bpb.sectors_per_fat ? wbpb->bpb.sectors_per_fat :
wbpb->bpb32.big_sectors_per_fat);
}
if (MakeFAT32) {
*fatsize = BPSEC * wbpb->bpb32.big_sectors_per_fat;
} else {
*fatsize = BPSEC * wbpb->bpb.sectors_per_fat;
}
if (!(fatp = (uchar_t *)malloc(*fatsize))) {
perror(gettext("FAT table alloc"));
exit(4);
} else {
(void) memset(fatp, 0, *fatsize);
}
/* Build in-memory FAT */
*fatp = wbpb->bpb.media;
*(fatp + 1) = 0xFF;
*(fatp + 2) = 0xFF;
if (Fatentsize == 16) {
*(fatp + 3) = 0xFF;
} else if (Fatentsize == 32) {
*(fatp + 3) = 0x0F;
*(fatp + 4) = 0xFF;
*(fatp + 5) = 0xFF;
*(fatp + 6) = 0xFF;
*(fatp + 7) = 0x0F;
}
/*
* Keep track of clusters used.
*/
remclust = TotalClusters;
nextfree = 2;
/*
* Get info on first file to install, if any.
*/
if (ffn)
*fffd = verify_firstfile(ffn, ffsize);
/*
* Compute number of clusters to preserve for bootblk overage.
* Remember that we already wrote the first sector of the boot block.
* These clusters are marked BAD to prevent them from being deleted
* or used. The first available cluster is 2, so we always offset
* the clusters.
*/
numsect = idivceil((bootblksize - BPSEC), BPSEC);
numclust = idivceil(numsect, wbpb->bpb.sectors_per_cluster);
if (Verbose && numclust)
(void) printf(gettext("Hiding %d excess bootblk cluster(s).\n"),
numclust);
for (ci = 0; ci < numclust; ci++)
mark_cluster(fatp, nextfree++,
MakeFAT32 ? PCF_BADCLUSTER32 : PCF_BADCLUSTER);
remclust -= numclust;
/*
* Reserve a cluster for the root directory on a FAT32.
*/
if (MakeFAT32) {
mark_cluster(fatp, nextfree, PCF_LASTCLUSTER32);
wbpb->bpb32.root_dir_clust = nextfree++;
remclust--;
}
/*
* Compute and preserve number of clusters for first file.
*/
if (*fffd >= 0) {
*ffstartclust = nextfree;
numsect = idivceil(*ffsize, BPSEC);
numclust = idivceil(numsect, wbpb->bpb.sectors_per_cluster);
if (numclust > remclust) {
(void) fprintf(stderr,
gettext("Requested first file too large to be\n"
"installed in the new file system.\n"));
(void) close(*fffd);
*fffd = -1;
goto finish;
}
if (Verbose)
(void) printf(gettext("Reserving %d first file "
"cluster(s).\n"), numclust);
for (ci = 0; (int)ci < (int)(numclust-1); ci++, nextfree++)
mark_cluster(fatp, nextfree, nextfree + 1);
mark_cluster(fatp, nextfree++,
MakeFAT32 ? PCF_LASTCLUSTER32 : PCF_LASTCLUSTER);
remclust -= numclust;
}
finish:
if (Verbose) {
(void) printf(gettext("First sector of FAT"));
header_for_dump();
dump_bytes(fatp, BPSEC);
}
(void) memset(fsinfop, 0, sizeof (*fsinfop));
fsinfop->fsi_leadsig = LE_32(FSI_LEADSIG);
fsinfop->fsi_strucsig = LE_32(FSI_STRUCSIG);
fsinfop->fsi_trailsig = LE_32(FSI_TRAILSIG);
fsinfop->fsi_incore.fs_free_clusters = LE_32(remclust);
fsinfop->fsi_incore.fs_next_free = LE_32(nextfree);
return (fatp);
}
static
void
dirent_time_fill(struct pcdir *dep)
{
struct timeval tv;
struct tm *tp;
ushort_t dostime;
ushort_t dosday;
(void) gettimeofday(&tv, (struct timezone *)0);
tp = localtime(&tv.tv_sec);
/* get the time & day into DOS format */
dostime = tp->tm_sec / 2;
dostime |= tp->tm_min << 5;
dostime |= tp->tm_hour << 11;
dosday = tp->tm_mday;
dosday |= (tp->tm_mon + 1) << 5;
dosday |= (tp->tm_year - 80) << 9;
dep->pcd_mtime.pct_time = htols(dostime);
dep->pcd_mtime.pct_date = htols(dosday);
}
static
void
dirent_label_fill(struct pcdir *dep, char *fn)
{
int nl, i;
/*
* We spread the volume label across both the NAME and EXT fields
*/
nl = min(PCFNAMESIZE, strlen(fn));
for (i = 0; i < nl; i++) {
dep->pcd_filename[i] = toupper(fn[i]);
}
if (i < PCFNAMESIZE) {
for (; i < PCFNAMESIZE; i++)
dep->pcd_filename[i] = ' ';
for (i = 0; i < PCFEXTSIZE; i++)
dep->pcd_ext[i] = ' ';
return;
}
nl = min(PCFEXTSIZE, strlen(fn) - PCFNAMESIZE);
for (i = 0; i < nl; i++)
dep->pcd_ext[i] = toupper(fn[i + PCFNAMESIZE]);
if (i < PCFEXTSIZE) {
for (; i < PCFEXTSIZE; i++)
dep->pcd_ext[i] = ' ';
}
}
static
void
dirent_fname_fill(struct pcdir *dep, char *fn)
{
char *fname, *fext;
int nl, i;
if (fname = strrchr(fn, '/')) {
fname++;
} else {
fname = fn;
}
if (fext = strrchr(fname, '.')) {
fext++;
} else {
fext = "";
}
fname = strtok(fname, ".");
nl = min(PCFNAMESIZE, (int)strlen(fname));
for (i = 0; i < nl; i++) {
dep->pcd_filename[i] = toupper(fname[i]);
}
for (; i < PCFNAMESIZE; i++) {
dep->pcd_filename[i] = ' ';
}
nl = min(PCFEXTSIZE, (int)strlen(fext));
for (i = 0; i < nl; i++) {
dep->pcd_ext[i] = toupper(fext[i]);
}
for (; i < PCFEXTSIZE; i++) {
dep->pcd_ext[i] = ' ';
}
}
static
uchar_t *
build_rootdir(bpb_t *wbpb, char *ffn, int fffd,
ulong_t ffsize, pc_cluster32_t ffstart, ulong_t *rdirsize)
{
struct pcdir *rootdirp;
struct pcdir *entry;
/*
* Build a root directory. It will have at least one entry,
* the volume label and a second if the first file was defined.
*/
if (MakeFAT32) {
/*
* We devote an entire cluster to the root
* directory on FAT32.
*/
*rdirsize = wbpb->bpb.sectors_per_cluster * BPSEC;
} else {
*rdirsize = wbpb->bpb.num_root_entries * sizeof (struct pcdir);
}
if ((rootdirp = (struct pcdir *)malloc(*rdirsize)) == NULL) {
perror(gettext("Root directory allocation"));
exit(4);
} else {
entry = rootdirp;
(void) memset((char *)rootdirp, 0, *rdirsize);
}
/* Create directory entry for first file, if there is one */
if (fffd >= 0) {
dirent_fname_fill(entry, ffn);
entry->pcd_attr = Firstfileattr;
dirent_time_fill(entry);
entry->pcd_scluster_lo = htols(ffstart);
if (MakeFAT32) {
ffstart = ffstart >> 16;
entry->un.pcd_scluster_hi = htols(ffstart);
}
entry->pcd_size = htoli(ffsize);
entry++;
}
/* Create directory entry for volume label, if there is one */
if (Label != NULL) {
dirent_label_fill(entry, Label);
entry->pcd_attr = PCA_ARCH | PCA_LABEL;
dirent_time_fill(entry);
entry->pcd_scluster_lo = 0;
if (MakeFAT32) {
entry->un.pcd_scluster_hi = 0;
}
entry->pcd_size = 0;
entry++;
}
if (Verbose) {
(void) printf(gettext("First two directory entries"));
header_for_dump();
dump_bytes((uchar_t *)rootdirp, 2 * sizeof (struct pcdir));
}
return ((uchar_t *)rootdirp);
}
/*
* write_rest
*
* Write all the bytes from the current file pointer to end of file
* in the source file out to the destination file. The writes should
* be padded to whole clusters with 0's if necessary.
*/
static
void
write_rest(bpb_t *wbpb, char *efn, int dfd, int sfd, int remaining)
{
char buf[BPSEC];
ushort_t numsect, numclust;
ushort_t wnumsect, s;
int doneread = 0;
int rstat;
/*
* Compute number of clusters required to contain remaining bytes.
*/
numsect = idivceil(remaining, BPSEC);
numclust = idivceil(numsect, wbpb->bpb.sectors_per_cluster);
wnumsect = numclust * wbpb->bpb.sectors_per_cluster;
for (s = 0; s < wnumsect; s++) {
if (!doneread) {
if ((rstat = read(sfd, buf, BPSEC)) < 0) {
perror(efn);
doneread = 1;
rstat = 0;
} else if (rstat == 0) {
doneread = 1;
}
(void) memset(&(buf[rstat]), 0, BPSEC - rstat);
}
if (write(dfd, buf, BPSEC) != BPSEC) {
(void) fprintf(stderr, gettext("Copying "));
perror(efn);
}
}
}
static
void
write_fat32_bootstuff(int fd, boot_sector_t *bsp,
struct fat_od_fsi *fsinfop, off64_t seekto)
{
if (Verbose) {
(void) printf(gettext("Dump of the fs info sector"));
header_for_dump();
dump_bytes((uchar_t *)fsinfop, sizeof (*fsinfop));
}
if (!Notreally) {
/*
* FAT32's have an FS info sector, then a backup of the boot
* sector, and a modified backup of the FS Info sector.
*/
if (write(fd, fsinfop, sizeof (*fsinfop)) != BPSEC) {
perror(gettext("FS info sector write"));
exit(4);
}
if (lseek64(fd, seekto + BKUP_BOOTSECT_OFFSET, SEEK_SET) < 0) {
(void) close(fd);
perror(gettext("Boot sector backup seek"));
exit(4);
}
if (write(fd, bsp->buf, sizeof (bsp->buf)) != BPSEC) {
perror(gettext("Boot sector backup write"));
exit(4);
}
}
/*
* Second copy of fs info sector is modified to have "don't know"
* as the number of free clusters
*/
fsinfop->fsi_incore.fs_next_free = LE_32(FSINFO_UNKNOWN);
if (Verbose) {
(void) printf(gettext("Dump of the backup fs info sector"));
header_for_dump();
dump_bytes((uchar_t *)fsinfop, sizeof (*fsinfop));
}
if (!Notreally) {
if (write(fd, fsinfop, sizeof (*fsinfop)) != BPSEC) {
perror(gettext("FS info sector backup write"));
exit(4);
}
}
}
static
void
write_bootsects(int fd, boot_sector_t *bsp, bpb_t *wbpb,
struct fat_od_fsi *fsinfop, off64_t seekto)
{
if (MakeFAT32) {
/* Copy our BPB into bootsec structure */
#ifdef i386
(void) memcpy(&(bsp->bs32.bs_front.bs_bpb), &(wbpb->bpb),
sizeof (wbpb->bpb));
(void) memcpy(&(bsp->bs32.bs_bpb32), &(wbpb->bpb32),
sizeof (wbpb->bpb32));
(void) memcpy(&(bsp->bs32.bs_ebpb), &(wbpb->ebpb),
sizeof (wbpb->ebpb));
#else
swap_pack_bpb32cpy(&(bsp->bs32), wbpb);
#endif
} else {
/* Copy our BPB into bootsec structure */
#ifdef i386
(void) memcpy(&(bsp->bs.bs_front.bs_bpb), &(wbpb->bpb),
sizeof (wbpb->bpb));
(void) memcpy(&(bsp->bs.bs_ebpb), &(wbpb->ebpb),
sizeof (wbpb->ebpb));
#else
swap_pack_bpbcpy(&(bsp->bs), wbpb);
#endif
/* Copy SUN BPB extensions into bootsec structure */
if (SunBPBfields) {
#ifdef i386
(void) memcpy(&(bsp->bs.bs_sebpb), &(wbpb->sunbpb),
sizeof (wbpb->sunbpb));
#else
swap_pack_sebpbcpy(&(bsp->bs), wbpb);
#endif
}
}
/* Write boot sector */
if (!Notreally && write(fd, bsp->buf, sizeof (bsp->buf)) != BPSEC) {
perror(gettext("Boot sector write"));
exit(4);
}
if (Verbose) {
(void) printf(gettext("Dump of the boot sector"));
header_for_dump();
dump_bytes(bsp->buf, sizeof (bsp->buf));
}
if (MakeFAT32)
write_fat32_bootstuff(fd, bsp, fsinfop, seekto);
}
static
void
write_fat(int fd, off64_t seekto, char *fn, char *lbl, char *ffn, bpb_t *wbpb)
{
struct fat_od_fsi fsinfo;
pc_cluster32_t ffsc;
boot_sector_t bootsect;
uchar_t *fatp, *rdirp;
ulong_t bootblksize, fatsize, rdirsize, ffsize;
int bsfd = -1;
int fffd = -1;
compute_file_area_size(wbpb);
bsfd = copy_bootblk(fn, &bootsect, &bootblksize);
label_volume(lbl, wbpb);
if (Verbose)
(void) printf(gettext("Building FAT.\n"));
fatp = build_fat(wbpb, &fsinfo, bootblksize, &fatsize,
ffn, &fffd, &ffsize, &ffsc);
write_bootsects(fd, &bootsect, wbpb, &fsinfo, seekto);
if (lseek64(fd,
seekto + (BPSEC * wbpb->bpb.resv_sectors), SEEK_SET) < 0) {
(void) close(fd);
perror(gettext("Seek to end of reserved sectors"));
exit(4);
}
/* Write FAT */
if (Verbose)
(void) printf(gettext("Writing FAT(s). %d bytes times %d.\n"),
fatsize, wbpb->bpb.num_fats);
if (!Notreally) {
int nf, wb;
for (nf = 0; nf < (int)wbpb->bpb.num_fats; nf++)
if ((wb = write(fd, fatp, fatsize)) != fatsize) {
perror(gettext("FAT write"));
exit(4);
} else {
if (Verbose)
(void) printf(
gettext("Wrote %d bytes\n"), wb);
}
}
free(fatp);
if (Verbose)
(void) printf(gettext("Building root directory.\n"));
rdirp = build_rootdir(wbpb, ffn, fffd, ffsize, ffsc, &rdirsize);
/*
* In non FAT32, root directory exists outside of the file area
*/
if (!MakeFAT32) {
if (Verbose)
(void) printf(gettext("Writing root directory. "
"%d bytes.\n"), rdirsize);
if (!Notreally) {
if (write(fd, rdirp, rdirsize) != rdirsize) {
perror(gettext("Root directory write"));
exit(4);
}
}
free(rdirp);
}
/*
* Now write anything that needs to be in the file space.
*/
if (bootblksize > BPSEC) {
if (Verbose)
(void) printf(gettext("Writing remainder of "
"boot block.\n"));
if (!Notreally)
write_rest(wbpb, fn, fd, bsfd, bootblksize - BPSEC);
}
if (MakeFAT32) {
if (Verbose)
(void) printf(gettext("Writing root directory. "
"%d bytes.\n"), rdirsize);
if (!Notreally) {
if (write(fd, rdirp, rdirsize) != rdirsize) {
perror(gettext("Root directory write"));
exit(4);
}
}
free(rdirp);
}
if (fffd >= 0) {
if (Verbose)
(void) printf(gettext("Writing first file.\n"));
if (!Notreally)
write_rest(wbpb, ffn, fd, fffd, ffsize);
}
}
static
char *LegalOpts[] = {
#define NFLAG 0
"N",
#define VFLAG 1
"v",
#define RFLAG 2
"r",
#define HFLAG 3
"h",
#define SFLAG 4
"s",
#define SUNFLAG 5
"S",
#define LABFLAG 6
"b",
#define BTRFLAG 7
"B",
#define INITFLAG 8
"i",
#define SZFLAG 9
"size",
#define SECTFLAG 10
"nsect",
#define TRKFLAG 11
"ntrack",
#define SPCFLAG 12
"spc",
#define BPFFLAG 13
"fat",
#define FFLAG 14
"f",
#define DFLAG 15
"d",
#define NOFDISKFLAG 16
"nofdisk",
#define RESRVFLAG 17
"reserve",
#define HIDDENFLAG 18
"hidden",
NULL
};
static
void
bad_arg(char *option)
{
(void) fprintf(stderr,
gettext("Unrecognized option %s.\n"), option);
usage();
exit(2);
}
static
void
missing_arg(char *option)
{
(void) fprintf(stderr,
gettext("Option %s requires a value.\n"), option);
usage();
exit(3);
}
static
void
parse_suboptions(char *optsstr)
{
char *value;
int c;
while (*optsstr != '\0') {
switch (c = getsubopt(&optsstr, LegalOpts, &value)) {
case NFLAG:
Notreally++;
break;
case VFLAG:
Verbose++;
break;
case RFLAG:
Firstfileattr |= 0x01;
break;
case HFLAG:
Firstfileattr |= 0x02;
break;
case SFLAG:
Firstfileattr |= 0x04;
break;
case SUNFLAG:
SunBPBfields = 1;
break;
case LABFLAG:
if (value == NULL) {
missing_arg(LegalOpts[c]);
} else {
Label = value;
}
break;
case BTRFLAG:
if (value == NULL) {
missing_arg(LegalOpts[c]);
} else {
BootBlkFn = value;
}
break;
case INITFLAG:
if (value == NULL) {
missing_arg(LegalOpts[c]);
} else {
FirstFn = value;
}
break;
case SZFLAG:
if (value == NULL) {
missing_arg(LegalOpts[c]);
} else {
TotSize = atoi(value);
GetSize = 0;
}
break;
case SECTFLAG:
if (value == NULL) {
missing_arg(LegalOpts[c]);
} else {
SecPerTrk = atoi(value);
GetSPT = 0;
}
break;
case TRKFLAG:
if (value == NULL) {
missing_arg(LegalOpts[c]);
} else {
TrkPerCyl = atoi(value);
GetTPC = 0;
}
break;
case SPCFLAG:
if (value == NULL) {
missing_arg(LegalOpts[c]);
} else {
SecPerClust = atoi(value);
GetSPC = 0;
}
break;
case BPFFLAG:
if (value == NULL) {
missing_arg(LegalOpts[c]);
} else {
BitsPerFAT = atoi(value);
GetBPF = 0;
}
break;
case NOFDISKFLAG:
DontUseFdisk = 1;
break;
case RESRVFLAG:
if (value == NULL) {
missing_arg(LegalOpts[c]);
} else {
Resrvd = atoi(value);
GetResrvd = 0;
}
break;
case HIDDENFLAG:
if (value == NULL) {
missing_arg(LegalOpts[c]);
} else {
RelOffset = atoi(value);
GetOffset = 0;
}
break;
case FFLAG:
if (value == NULL) {
missing_arg(LegalOpts[c]);
} else {
DiskName = value;
Outputtofile = 1;
}
break;
case DFLAG:
if (value == NULL) {
missing_arg(LegalOpts[c]);
} else {
Imagesize = atoi(value);
}
break;
default:
bad_arg(value);
break;
}
}
}
static
void
sanity_check_options(int argc, int optind)
{
if (GetFsParams) {
if (argc - optind != 1)
usage();
return;
}
if (DontUseFdisk && GetOffset) {
/* Set default relative offset of zero */
RelOffset = 0;
}
if (BitsPerFAT == 32)
MakeFAT32 = 1;
if (Outputtofile && (argc - optind)) {
usage();
} else if (Outputtofile && !DiskName) {
usage();
} else if (!Outputtofile && (argc - optind != 1)) {
usage();
} else if (SunBPBfields && !BootBlkFn) {
(void) fprintf(stderr,
gettext("Use of the 'S' option requires that\n"
"the 'B=' option also be used.\n\n"));
usage();
} else if (Firstfileattr != 0x20 && !FirstFn) {
(void) fprintf(stderr,
gettext("Use of the 'r', 'h', or 's' options requires\n"
"that the 'i=' option also be used.\n\n"));
usage();
} else if (!GetOffset && !DontUseFdisk) {
(void) fprintf(stderr,
gettext("Use of the 'hidden' option requires that\n"
"the 'nofdisk' option also be used.\n\n"));
usage();
} else if (DontUseFdisk && GetSize) {
(void) fprintf(stderr,
gettext("Use of the 'nofdisk' option requires that\n"
"the 'size=' option also be used.\n\n"));
usage();
} else if (!GetBPF &&
BitsPerFAT != 12 && BitsPerFAT != 16 && BitsPerFAT != 32) {
(void) fprintf(stderr, gettext("Invalid Bits/Fat value."
" Must be 12, 16 or 32.\n"));
exit(2);
} else if (!GetSPC && !(ISP2(SecPerClust) &&
IN_RANGE(SecPerClust, 1, 128))) {
(void) fprintf(stderr,
gettext("Invalid Sectors/Cluster value. Must be a "
"power of 2 between 1 and 128.\n"));
exit(2);
} else if (!GetResrvd && (Resrvd < 1 || Resrvd > 0xffff)) {
(void) fprintf(stderr,
gettext("Invalid number of reserved sectors. "
"Must be at least 1 but\nno larger than 65535."));
exit(2);
} else if (!GetResrvd && MakeFAT32 &&
(Resrvd < 32 || Resrvd > 0xffff)) {
(void) fprintf(stderr,
gettext("Invalid number of reserved sectors. "
"Must be at least 32 but\nno larger than 65535."));
exit(2);
} else if (Imagesize != 3 && Imagesize != 5) {
usage();
}
}
int
main(int argc, char **argv)
{
off64_t AbsBootSect = 0;
bpb_t dskparamblk;
char *string;
int fd;
int c;
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
while ((c = getopt(argc, argv, "F:Vmo:")) != EOF) {
switch (c) {
case 'F':
string = optarg;
if (strcmp(string, "pcfs") != 0)
usage();
break;
case 'V':
{
char *opt_text;
int opt_count;
(void) fprintf(stdout,
gettext("mkfs -F pcfs "));
for (opt_count = 1; opt_count < argc;
opt_count++) {
opt_text = argv[opt_count];
if (opt_text)
(void) fprintf(stdout,
" %s ", opt_text);
}
(void) fprintf(stdout, "\n");
}
break;
case 'm':
GetFsParams++;
break;
case 'o':
string = optarg;
parse_suboptions(string);
break;
}
}
sanity_check_options(argc, optind);
if (!Outputtofile)
DiskName = argv[optind];
(void) memset(&dskparamblk, 0, sizeof (dskparamblk));
if (GetFsParams) {
fd = open_and_examine(DiskName, &dskparamblk);
} else {
fd = open_and_seek(DiskName, &dskparamblk, &AbsBootSect);
if (ask_nicely(DiskName))
write_fat(fd, AbsBootSect, BootBlkFn, Label,
FirstFn, &dskparamblk);
}
(void) close(fd);
return (0);
}
|