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
|
/*
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* fdformat program - formats floppy disks, and then adds a label to them
*
* ****Warning, Warning, Warning, Warning*****
* This program runs suid root. This change was made to
* allow it to umount a file system if it's mounted.
*/
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <memory.h>
#include <errno.h>
#include <locale.h>
#include <libintl.h>
#include <volmgt.h>
#include <sys/isa_defs.h>
#include <sys/ioccom.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/file.h>
#include <sys/dklabel.h>
#include <sys/ioctl.h>
#include <sys/dkio.h>
#include <sys/fdio.h>
#include <sys/stat.h>
#include <sys/vtoc.h>
#include <sys/mnttab.h>
/* DEFINES */
#if defined(_BIG_ENDIAN)
#define getbyte(A, N) (((unsigned char *)(&(A)))[N])
#define htols(S) ((getbyte(S, 1) <<8) | getbyte(S, 0))
#elif defined(_LITTLE_ENDIAN)
#define htols(S) (*((ushort_t *)(&(S))))
#else
#error One of _BIG_ENDIAN or LITTLE_ENDIAN must be defined
#endif
#define getlobyte(A) (A & 0xFF)
#define gethibyte(A) (A >> 8 & 0xFF)
#define uppercase(c) ((c) >= 'a' && (c) <= 'z' ? (c) - 'a' + 'A' : (c))
#define min(a, b) ((a) < (b) ? (a) : (b))
/* FORMAT PATTERNS */
#define PATTERN_1 0x55;
#define PATTERN_2 0xaa;
#define PATTERN_3 0xff;
#define PATTERN_4 0x00;
/* UNINITIALIZED DATA */
static struct fd_char fdchar;
static struct dk_geom fdgeom;
static struct dk_allmap allmap;
static struct dk_cinfo dkinfo;
/* EXTERN */
extern char *optarg;
extern int optind;
/* for verify buffers */
static uchar_t *ibuf1;
static uchar_t *obuf;
static char *myname;
static int fd_debug = 1; /* 1 if debug XXX */
static int b_flag = 0; /* install a volume label to the diskette */
static int d_flag = 0; /* format the diskette in dos format */
static int D_flag = 0; /* double (aka low) density flag */
static int e_flag = 0; /* "eject" diskette when done (if supported) */
static int E_flag = 0; /* extended density */
static int f_flag = 0; /* "force" (no confirmation before start) */
static int H_flag = 0; /* high density */
static int m_flag = 0; /* medium density */
static int n_flag = 0; /* format the diskette in NEC-DOS format */
static int q_flag = 0; /* quiet format flag */
static int U_flag = 0; /* automatically unmount if it's mounted */
static int v_flag = 0; /* verify format/diskette flag */
static int x_flag = 0; /* skip the format, only install SunOS label */
/* or DOS file system */
static int z_flag = 0; /* debugging only, setting partial formatting */
static int interleave = 1; /* interleave factor */
static uid_t euid = 0; /* stores effective user id */
struct bios_param_blk {
uchar_t b_bps[2]; /* bytes per sector */
uchar_t b_spcl; /* sectors per alloction unit */
uchar_t b_res_sec[2]; /* reserved sectors, starting at 0 */
uchar_t b_nfat; /* number of FATs */
uchar_t b_rdirents[2]; /* number of root directory entries */
uchar_t b_totalsec[2]; /* total sectors in logical image */
char b_mediadescriptor; /* media descriptor byte */
uchar_t b_fatsec[2]; /* number of sectors per FAT */
uchar_t b_spt[2]; /* sectors per track */
uchar_t b_nhead[2]; /* number of heads */
uchar_t b_hiddensec[2]; /* number of hidden sectors */
};
/*
* ON-private functions from libvolmgt
*/
char *_media_oldaliases(char *name);
int _dev_mounted(char *path);
int _dev_unmount(char *path);
/*
* local functions
*/
static void usage(char *);
static int verify(int, int, int);
static void write_SunOS_label(int, char *, struct vtoc *);
static int valid_DOS_boot(char *, uchar_t **);
static void write_DOS_label(int, uchar_t *, int, char *, char *,
struct bios_param_blk *, int);
static void write_NEC_DOS_label(int, char *);
static int check_mount();
static void format_diskette(int, char *, struct vtoc *,
struct bios_param_blk *, int *);
static void restore_default_chars(int fd,
struct fd_char save_fdchar,
struct dk_allmap save_allmap);
int
main(int argc, char **argv)
{
int altsize = 0;
int fd;
int i;
uchar_t *altboot = NULL;
char *altbootname = NULL;
char *dev_name = NULL, *real_name, *alias_name;
char *vollabel = "";
struct vtoc fd_vtoc;
struct bios_param_blk bpb;
int rdirsec;
char *nullstring = "";
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
myname = argv[0];
while ((i = getopt(argc, argv, "B:b:dDeEfhHlLmMxqt:UvVZ?")) != -1) {
switch (i) {
case 'B':
altbootname = strdup(optarg);
d_flag++;
/* check for valid boot file now */
altsize = valid_DOS_boot(altbootname, &altboot);
if (!altsize) {
(void) fprintf(stderr, gettext(
"%s: invalid boot loader\n"), myname);
exit(1);
}
break;
case 'b':
b_flag++;
vollabel = strdup(optarg);
break;
case 'd':
/* format a MS-DOS diskette */
d_flag++;
break;
case 'D':
case 'L':
case 'l':
/* format a Double density 720KB (or 360KB) disk */
D_flag++;
break;
case 'e':
/* eject diskette when done */
e_flag++;
break;
case 'E':
/* format an 2.88MB Extended density disk */
E_flag++;
break;
case 'f':
/* don't ask for confirmation */
f_flag++;
break;
case 'H':
case 'h':
/* format a High density 1.2MB or 1.44MB disk */
H_flag++;
break;
#if 0
case 'i':
/* interleave factor */
interleave = atol(optarg);
if (interleave <= 0) {
(void) fprintf(stderr, gettext(
"%s: invalid interleave\n"), myname);
exit(1);
}
break;
#endif
case 'M':
case 'm':
/* format a 3.5" HD disk to 1.2MB */
m_flag++;
break;
case 'x':
/* skip format, just write label */
x_flag++;
break;
case 'q':
/* quiet format */
q_flag++;
break;
case 't':
/* Type of DOS formatting: NEC or MS */
if (strcmp(optarg, "nec") == 0) {
n_flag++;
}
if (strcmp(optarg, "dos") == 0) {
d_flag++;
}
break;
case 'U':
/* umount filesystem if mounted */
U_flag++;
break;
case 'v':
case 'V':
/* verify the diskette after format */
v_flag++;
break;
case 'Z':
/* for debug only, format cyl 0 only */
if (!fd_debug) {
usage(gettext("unknown argument"));
/* NOTREACHED */
}
(void) printf(gettext("\nFormat cyl Zero only\n"));
z_flag++;
break;
default:
usage(" ");
/* NOTREACHED */
}
}
if (optind < argc -1) {
usage(gettext("more than one device name argument"));
/* NOTREACHED */
}
if (optind == argc -1) {
dev_name = argv[optind];
}
if (D_flag && H_flag) {
usage(gettext("switches -D, -L and -H incompatible"));
/* NOTREACHED */
}
if (D_flag && E_flag) {
usage(gettext("switches -D, -L and -E incompatible"));
/* NOTREACHED */
}
if (H_flag && E_flag) {
usage(gettext("switches -H and -E incompatible"));
/* NOTREACHED */
}
if (n_flag && d_flag) {
usage(gettext("switches nec and dos incompatible"));
/* NOTREACHED */
}
if (n_flag && !m_flag) {
usage(gettext("switch -M required for NEC-DOS"));
/* NOTREACHED */
}
if (D_flag && m_flag) {
usage(gettext("switches -D, -L and -M incompatible"));
/* NOTREACHED */
}
if (d_flag && m_flag) {
usage(gettext("switches -d and -M incompatible"));
/* NOTREACHED */
}
if (dev_name == NULL)
dev_name = "floppy";
if ((real_name = media_findname(dev_name)) == NULL) {
if ((alias_name = _media_oldaliases(dev_name)) != NULL)
real_name = media_findname(alias_name);
if (real_name == NULL) {
(void) fprintf(stderr,
gettext("No such volume (or no media in specified device): %s\n"),
dev_name);
exit(1);
}
}
/*
* This check is required because program runs suid root.
*/
if (access(real_name, R_OK|W_OK) < 0) {
perror(real_name);
exit(1);
}
/* store callers euid */
euid = geteuid();
/*
* See if the given device name is mounted. If this check isn't done
* before the open, the open will fail. The failed open will not
* indicate that the device is mounted, only that it's busy
*/
if (_dev_mounted(real_name)) {
if (U_flag) {
if (!_dev_unmount(real_name)) {
(void) fprintf(stderr,
gettext("%s: umount of %s failed\n"),
myname, real_name);
exit(1);
}
} else {
(void) fprintf(stderr,
gettext("%s: %s is mounted (use -U flag)\n"),
myname, real_name);
exit(1);
}
}
/* Set to user access permissions to open file */
(void) seteuid(getuid());
if ((fd = open(real_name, O_NDELAY | O_RDWR | O_EXCL)) == -1) {
if (errno == EROFS) {
(void) fprintf(stderr,
gettext("%s: \"%s\" is write protected\n"),
myname, real_name);
exit(1);
}
/* XXX ought to check for "drive not installed", etc. */
(void) fprintf(stderr, gettext("%s: could not open \"%s\": "),
myname, real_name);
perror(nullstring);
exit(1);
}
/* restore effective id */
(void) seteuid(euid);
if (ioctl(fd, DKIOCINFO, &dkinfo) < 0) {
(void) fprintf(stderr,
gettext("%s: DKIOCINFO failed, "), myname);
perror(nullstring);
exit(3);
}
/* See if there are any mounted partitions. */
if (check_mount() != 0) {
exit(3);
}
/*
* The fd_vtoc, bpb, and rdirsec structures will be
* partially filled in by format_diskette().
* This was done so that write_DOS_label(),
* write_SunOS_label(), and write_NEC_DOS_label() could be
* device independent. If a new device needs to be added to
* fdformat, a new format function like format_diskette should
* be added. This function should fill in fd_vtoc, bpb, and
* rdirsec with device dependent information.
*/
(void) memset((void *)&fd_vtoc, (char)0, sizeof (struct vtoc));
(void) memset((void *)&bpb, (char)0, sizeof (struct bios_param_blk));
format_diskette(fd, real_name, &fd_vtoc, &bpb, &rdirsec);
if (d_flag)
write_DOS_label(fd, altboot, altsize, altbootname,
vollabel, &bpb, rdirsec);
else if (n_flag)
write_NEC_DOS_label(fd, vollabel);
else
write_SunOS_label(fd, vollabel, &fd_vtoc);
if (e_flag)
/* eject media if possible */
if (ioctl(fd, FDEJECT, 0)) {
(void) fprintf(stderr,
gettext("%s: could not eject diskette, "), myname);
perror(nullstring);
exit(3);
}
return (0);
}
/*
* Inputs: file descriptor for the device and the device name.
* Oututs: the fd_vtoc will be partially filled in with the
* device specific information such as partition
* information and ascillabel. bpb and rdirsec will
* also be partially filled in with device specific information
*/
void
format_diskette(int fd, char *real_name, struct vtoc *fd_vtoc,
struct bios_param_blk *bpb, int *rdirsec)
{
int transfer_rate = 1000; /* transfer rate code */
int sec_size = 512; /* sector size */
uchar_t gap = 0x54; /* format gap size */
uchar_t *fbuf, *p;
char *capacity = NULL;
int cyl_size;
int i;
int chgd; /* for testing disk changed/present */
int cyl, hd;
int size_of_part, size_of_dev;
int spt = 36; /* sectors per track */
int drive_size;
uchar_t num_cyl = 80; /* max number of cylinders */
char *nullstring = "";
struct fd_char save_fdchar; /* original diskette characteristics */
struct dk_allmap save_allmap; /* original diskette partition info */
/* FDRAW ioctl command structures for seeking and formatting */
struct fd_raw fdr_seek = {
FDRAW_SEEK, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0
};
struct fd_raw fdr_form = {
0x4D, 0, 2, 0, 0x54, (char)0xA5, 0, 0, 0, 0,
6,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, /* nbytes */
0 /* addr */
};
/*
* restore drive to default geometry and characteristics
* (probably not implemented on sparc)
*/
(void) ioctl(fd, FDDEFGEOCHAR, NULL);
/* get the default partititon maps */
if (ioctl(fd, DKIOCGAPART, &allmap) == -1) {
(void) fprintf(stderr,
gettext("%s: DKIOCGAPART failed, "), myname);
perror(nullstring);
exit(3);
}
/* Save the original default partition maps */
save_allmap = allmap;
/* find out the characteristics of the default diskette */
if (ioctl(fd, FDIOGCHAR, &fdchar) == -1) {
(void) fprintf(stderr,
gettext("%s: FDIOGCHAR failed, "), myname);
perror(nullstring);
exit(3);
}
/* Save the original characteristics of the default diskette */
save_fdchar = fdchar;
/*
* The user may only format the entire diskette.
* formatting partion a or b is not allowed
*/
size_of_part = allmap.dka_map[dkinfo.dki_partition].dkl_nblk
* DEV_BSIZE;
size_of_dev = fdchar.fdc_ncyl * fdchar.fdc_nhead
* fdchar.fdc_secptrack * fdchar.fdc_sec_size;
if (size_of_part != size_of_dev) {
(void) fprintf(stderr,
/*CSTYLED*/
gettext("%s: The entire diskette must be formatted. Invalid device name.\n"),
myname);
exit(3);
}
/* find out the geometry of the drive */
if (ioctl(fd, DKIOCGGEOM, &fdgeom) == -1) {
(void) fprintf(stderr,
gettext("%s: DKIOCGGEOM failed, "), myname);
perror(nullstring);
exit(3);
}
#ifdef sparc
fdchar.fdc_medium = 3;
#endif
if (fdchar.fdc_medium == 5)
drive_size = 5;
else
drive_size = 3;
/*
* set proper density flag in case we're formating to default
* characteristics because no density switch was input
*/
if ((E_flag | H_flag | D_flag | m_flag) == 0) {
switch (fdchar.fdc_transfer_rate) {
case 1000:
/* assumes only ED uses 1.0 MB/sec */
E_flag++;
break;
case 500:
default:
/*
* default to HD even though High density and
* "medium" density both use 500 KB/sec
*/
H_flag++;
break;
#ifndef sparc
case 250:
/* assumes only DD uses 250 KB/sec */
D_flag++;
break;
#endif
}
}
if (H_flag) {
transfer_rate = 500;
num_cyl = 80;
sec_size = 512;
if (drive_size == 5) {
(void) strcpy(fd_vtoc->v_asciilabel,
"5.25\" floppy cyl 80 alt 0 hd 2 sec 15");
spt = 15;
capacity = "1.2 MB";
} else {
(void) strcpy(fd_vtoc->v_asciilabel,
"3.5\" floppy cyl 80 alt 0 hd 2 sec 18");
spt = 18;
capacity = "1.44 MB";
}
gap = 0x54;
} else if (D_flag) {
transfer_rate = 250;
if (drive_size == 5) {
(void) strcpy(fd_vtoc->v_asciilabel,
"5.25\" floppy cyl 40 alt 0 hd 2 sec 9");
if (fdchar.fdc_transfer_rate == 500) {
/*
* formatting a 360KB DD diskette in
* a 1.2MB drive is not a good idea
*/
transfer_rate = 300;
fdchar.fdc_steps = 2;
}
num_cyl = 40;
gap = 0x50;
capacity = "360 KB";
} else {
(void) strcpy(fd_vtoc->v_asciilabel,
"3.5\" floppy cyl 80 alt 0 hd 2 sec 9");
num_cyl = 80;
gap = 0x54;
capacity = "720 KB";
}
sec_size = 512;
spt = 9;
} else if (m_flag) {
#ifdef sparc
transfer_rate = 500;
#else
/*
* 416.67 KB/sec is the effective transfer rate of a "medium"
* density diskette spun at 300 rpm instead of 360 rpm
*/
transfer_rate = 417;
#endif
(void) strcpy(fd_vtoc->v_asciilabel,
"3.5\" floppy cyl 77 alt 0 hd 2 sec 8");
num_cyl = 77;
sec_size = 1024;
spt = 8;
gap = 0x74;
capacity = "1.2 MB";
} else if (E_flag) {
(void) strcpy(fd_vtoc->v_asciilabel,
"3.5\" floppy cyl 80 alt 0 hd 2 sec 36");
transfer_rate = 1000;
num_cyl = 80;
sec_size = 512;
spt = 36;
gap = 0x54;
capacity = "2.88 MB";
}
/*
* Medium density diskettes have 1024 byte blocks. The dk_map
* structure in dklabel.h assumes the blocks size is DEVBSIZE (512)
* bytes. The dkl_nblk field is in terms of DEVBSIZE byte blocks
* while the spt variable is in terms of the true block size on
* the diskette.
*/
if (allmap.dka_map[2].dkl_nblk !=
(2 * num_cyl * spt * (m_flag ? 2 : 1))) {
allmap.dka_map[1].dkl_cylno = num_cyl - 1;
allmap.dka_map[0].dkl_nblk = 2 * (num_cyl - 1) * spt *
(m_flag ? 2 : 1);
allmap.dka_map[1].dkl_nblk = 2 * spt * (m_flag ? 2 : 1);
allmap.dka_map[2].dkl_nblk = 2 * num_cyl * spt *
(m_flag ? 2 : 1);
if (allmap.dka_map[3].dkl_nblk)
allmap.dka_map[3].dkl_nblk = 2 * (num_cyl - 1) * spt *
(m_flag ? 2 : 1);
if (allmap.dka_map[4].dkl_nblk)
allmap.dka_map[4].dkl_nblk =
2 * spt * (m_flag ? 2 : 1);
}
/* initialize the vtoc structure */
fd_vtoc->v_nparts = 3;
fd_vtoc->v_part[0].p_start = 0;
fd_vtoc->v_part[0].p_size = ((num_cyl - 1) * 2 * spt *
(m_flag ? 2 : 1));
fd_vtoc->v_part[1].p_start = ((num_cyl - 1) * 2 * spt *
(m_flag ? 2 : 1));
fd_vtoc->v_part[1].p_size = 2 * spt * (m_flag ? 2 : 1);
fd_vtoc->v_part[2].p_start = 0;
fd_vtoc->v_part[2].p_size = num_cyl * 2 * spt * (m_flag ? 2 : 1);
/* initialize the bios parameter blockstructure */
bpb->b_nfat = 2;
if (E_flag && drive_size == 3) {
bpb->b_spcl = 2;
*rdirsec = (ushort_t)240;
bpb->b_mediadescriptor = (char)0xF0;
bpb->b_fatsec[0] = 9;
bpb->b_fatsec[1] = 0;
} else if (H_flag) {
if (drive_size == 5) {
bpb->b_spcl = 1;
*rdirsec = 224;
bpb->b_mediadescriptor = (char)0xF9;
bpb->b_fatsec[0] = 7;
bpb->b_fatsec[1] = 0;
} else {
bpb->b_spcl = 1;
*rdirsec = 224;
bpb->b_mediadescriptor = (char)0xF0;
bpb->b_fatsec[0] = 9;
bpb->b_fatsec[1] = 0;
}
} else if (drive_size == 5) {
bpb->b_spcl = 2;
*rdirsec = 112;
bpb->b_mediadescriptor = (char)0xFD;
bpb->b_fatsec[0] = 2;
bpb->b_fatsec[1] = 0;
} else if (drive_size == 3) {
bpb->b_spcl = 2;
*rdirsec = 112;
bpb->b_mediadescriptor = (char)0xF9;
bpb->b_fatsec[0] = 3;
bpb->b_fatsec[1] = 0;
}
#ifndef sparc
if (num_cyl > fdchar.fdc_ncyl || spt > fdchar.fdc_secptrack ||
transfer_rate > fdchar.fdc_transfer_rate) {
(void) fprintf(stderr,
gettext("%s: drive not capable of requested density, "),
myname);
perror(nullstring);
exit(3);
}
#endif
if (num_cyl != fdchar.fdc_ncyl || spt != fdchar.fdc_secptrack ||
transfer_rate != fdchar.fdc_transfer_rate) {
/*
* -- CAUTION --
* The SPARC fd driver is using a non-zero value in
* fdc_medium to indicate the 360 rpm, 77 track,
* 9 sectors/track, 1024 bytes/sector mode of operation
* (similar to an 8", DS/DD, 1.2 MB floppy).
*
* The x86 fd driver uses fdc_medium as the diameter
* indicator, either 3 or 5. It should not be modified.
*/
#ifdef sparc
fdchar.fdc_medium = m_flag ? 1 : 0;
#endif
fdchar.fdc_transfer_rate = transfer_rate;
fdchar.fdc_ncyl = num_cyl;
fdchar.fdc_sec_size = sec_size;
fdchar.fdc_secptrack = spt;
if (ioctl(fd, FDIOSCHAR, &fdchar) == -1) {
(void) fprintf(stderr, gettext(
"%s: FDIOSCHAR (density selection) failed, "),
myname);
/* restore the default characteristics */
restore_default_chars(fd, save_fdchar, save_allmap);
perror(nullstring);
exit(3);
}
if (ioctl(fd, DKIOCSAPART, &allmap) == -1) {
(void) fprintf(stderr,
gettext("%s: DKIOCSAPART failed, "),
myname);
/* restore the default characteristics */
restore_default_chars(fd, save_fdchar, save_allmap);
perror(nullstring);
exit(3);
}
}
if (interleave != 1 && interleave != fdgeom.dkg_intrlv) {
fdgeom.dkg_intrlv = interleave;
if (ioctl(fd, DKIOCSGEOM, &fdgeom) == -1) {
(void) fprintf(stderr,
gettext("%s: DKIOCSGEOM failed, "), myname);
perror(nullstring);
/* restore the default characteristics */
restore_default_chars(fd, save_fdchar, save_allmap);
exit(3);
}
}
cyl_size = 2 * sec_size * spt;
if ((ibuf1 = (uchar_t *)malloc((size_t)cyl_size)) == 0 ||
(obuf = (uchar_t *)malloc((size_t)cyl_size)) == 0) {
(void) fprintf(stderr,
gettext("%s: can't malloc verify buffer, "),
myname);
perror(nullstring);
/* restore the default characteristics */
restore_default_chars(fd, save_fdchar, save_allmap);
exit(4);
}
(void) memset(ibuf1, (uchar_t)0xA5, cyl_size);
if (x_flag)
goto skipformat;
if (!(q_flag && f_flag)) {
if (interleave != 1) {
(void) printf(gettext(
"Formatting %s, %d cylinders, %d sectors per trk, interleave=%d in %s\n"),
capacity, num_cyl, spt, interleave, real_name);
} else {
(void) printf(gettext("Formatting %s in %s\n"),
capacity, real_name);
}
}
if (!f_flag) {
(void) printf(
gettext("Press return to start formatting floppy."));
while (getchar() != '\n')
;
}
/*
* for those systems that support this ioctl, they will
* return whether or not a diskette is in the drive.
*/
if (ioctl(fd, FDGETCHANGE, &chgd) == 0) {
if (chgd & FDGC_CURRENT) {
(void) fprintf(stderr,
gettext("%s: no diskette in drive %s\n"),
myname, real_name);
/* restore the default characteristics */
restore_default_chars(fd, save_fdchar, save_allmap);
exit(4);
}
if (chgd & FDGC_CURWPROT) {
(void) fprintf(stderr,
gettext("%s: \"%s\" is write protected\n"),
myname, real_name);
/* restore the default characteristics */
restore_default_chars(fd, save_fdchar, save_allmap);
exit(1);
}
}
if ((fbuf = (uchar_t *)malloc((unsigned)(4 * spt))) == 0) {
(void) fprintf(stderr,
gettext("%s: can't malloc format header buffer, "),
myname);
perror(nullstring);
/* restore the default characteristics */
restore_default_chars(fd, save_fdchar, save_allmap);
exit(3);
}
/*
* do the format, a track at a time
*/
for (cyl = 0; cyl < (z_flag ? 1 : (int)num_cyl); cyl++) {
/*
* This is not the optimal ioctl to format the floppy.
* The device driver should do do the work,
* instead of this program mucking with a lot
* of low-level, device-dependent code.
*/
fdr_seek.fdr_cmd[2] = cyl;
if (ioctl(fd, FDRAW, &fdr_seek) == -1) {
(void) fprintf(stderr,
gettext("%s: seek to cyl %d failed\n"),
myname, cyl);
/* restore the default characteristics */
restore_default_chars(fd, save_fdchar, save_allmap);
exit(3);
}
/*
* Assume that the fd driver has issued a SENSE_INT
* command to complete the seek operation.
*/
for (hd = 0; hd < fdchar.fdc_nhead; hd++) {
p = (uchar_t *)fbuf;
for (i = 1; i <= spt; i++) {
*p++ = cyl;
*p++ = hd;
*p++ = i; /* sector # */
*p++ = (sec_size == 1024) ? 3 : 2;
}
/*
* ASSUME the fd driver is going to set drive-select
* bits in the second command byte
*/
fdr_form.fdr_cmd[1] = hd << 2;
fdr_form.fdr_cmd[2] = (sec_size == 1024) ? 3 : 2;
fdr_form.fdr_cmd[3] = spt;
fdr_form.fdr_cmd[4] = gap;
fdr_form.fdr_nbytes = 4 * spt;
fdr_form.fdr_addr = (char *)fbuf;
if (ioctl(fd, FDRAW, &fdr_form) == -1) {
(void) fprintf(stderr, gettext(
"%s: format of cyl %d head %d failed\n"),
myname, cyl, hd);
/* restore the default characteristics */
restore_default_chars(fd, save_fdchar,
save_allmap);
exit(3);
}
if (fdr_form.fdr_result[0] & 0xC0) {
if (fdr_form.fdr_result[1] & 0x02) {
(void) fprintf(stderr, gettext(
/*CSTYLED*/
"%s: diskette is write protected\n"),
myname);
/*
* restore the default
* characteristics
*/
restore_default_chars(fd, save_fdchar,
save_allmap);
exit(3);
}
(void) fprintf(stderr, gettext(
"%s: format of cyl %d head %d failed\n"),
myname, cyl, hd);
/* restore the default characteristics */
restore_default_chars(fd, save_fdchar,
save_allmap);
exit(3);
}
}
/*
* do a quick verify
*/
if (!v_flag) {
if (lseek(fd, cyl * cyl_size, 0) != cyl * cyl_size) {
(void) fprintf(stderr,
gettext("%s: bad seek to format verify, "),
myname);
perror(nullstring);
/* restore the default characteristics */
restore_default_chars(fd, save_fdchar,
save_allmap);
exit(3);
}
if (read(fd, obuf, cyl_size) == cyl_size) {
/* write some progress msg */
/* when each cylinder is done. */
if (!q_flag)
(void) printf(".");
} else {
if (!q_flag)
(void) printf(gettext("e\n"));
(void) fprintf(stderr, gettext(
"%s: can't read format data, "), myname);
perror(nullstring);
/* restore the default characteristics */
restore_default_chars(fd, save_fdchar,
save_allmap);
exit(3);
}
} else
if (!q_flag)
(void) printf(".");
if (!q_flag)
(void) fflush(stdout);
}
if (!q_flag)
(void) printf("\n");
skipformat:
if (v_flag) {
/*
* do a write & read verify of the entire diskette
*/
if (!q_flag && x_flag)
(void) printf(gettext("Verifying %s in %s\n"),
capacity, real_name);
for (cyl = 0; cyl < (int)num_cyl; cyl++) {
int val;
if ((val = verify(fd, 2 * spt * cyl, cyl_size)) != 0) {
perror(nullstring);
/* restore the default characteristics */
restore_default_chars(fd, save_fdchar,
save_allmap);
exit(val);
}
/* write some progress msg as */
/* each cylinder is done. */
if (!q_flag)
(void) printf(gettext("v"));
(void) fflush(stdout);
}
if (!q_flag)
(void) printf("\n");
}
if (lseek(fd, (off_t)0, 0) != 0) {
(void) fprintf(stderr, gettext("%s: seek to blk 0 failed, "),
myname);
perror(nullstring);
/* restore the default characteristics */
restore_default_chars(fd, save_fdchar, save_allmap);
exit(3);
}
}
/*
* Restore the default characteristics of the floppy diskette.
* Fdformat changes the characteristics in the process of formatting.
* If fdformat fails while in the process of doing the format, fdformat
* should clean up after itself and reset the driver back to the original
* state.
*/
static void
restore_default_chars(int fd,
struct fd_char save_fdchar,
struct dk_allmap save_allmap)
{
/*
* When this function is called, fdformat is failing anyways,
* so the errors are not processed.
*/
(void) ioctl(fd, FDIOSCHAR, &save_fdchar);
(void) ioctl(fd, DKIOCSAPART, &save_allmap);
/*
* Before looking at the diskette's characteristics, format_diskette()
* sets the x86 floppy driver to the default characteristics.
* restore drive to default geometry and
* characteristics. This ioctl isn't implemented on
* sparc.
*/
(void) ioctl(fd, FDDEFGEOCHAR, NULL);
}
/*
* See if any partitions on the device are mounted. Return 1 if a partition is
* mounted. Return 0 otherwise.
*/
static int
check_mount()
{
FILE *fp = NULL;
int mfd;
struct dk_cinfo dkinfo_tmp;
struct mnttab mnt_record;
struct mnttab *mp = &mnt_record;
struct stat stbuf;
char raw_device[MAXPATHLEN];
int found = 0;
if ((fp = fopen(MNTTAB, "r")) == NULL) {
perror(MNTTAB);
exit(3);
}
while (getmntent(fp, mp) == 0) {
if (strstr(mp->mnt_special, "/dev/fd") == NULL &&
strstr(mp->mnt_special, "/dev/disket") == NULL &&
strstr(mp->mnt_special, "/dev/c") == NULL) {
continue;
}
(void) strcpy(raw_device, "/dev/r");
(void) strcat(raw_device, mp->mnt_special + strlen("/dev/"));
/*
* Attempt to open the device. If it fails, skip it.
*/
if ((mfd = open(raw_device, O_RDWR | O_NDELAY)) < 0) {
continue;
}
/*
* Must be a character device
*/
if (fstat(mfd, &stbuf) == -1 || !S_ISCHR(stbuf.st_mode)) {
(void) close(mfd);
continue;
}
/*
* Attempt to read the configuration info on the disk.
*/
if (ioctl(mfd, DKIOCINFO, &dkinfo_tmp) < 0) {
(void) close(mfd);
continue;
}
/*
* Finished with the opened device
*/
(void) close(mfd);
/*
* If it's not the disk we're interested in, it doesn't apply.
*/
if (dkinfo.dki_ctype != dkinfo_tmp.dki_ctype ||
dkinfo.dki_cnum != dkinfo_tmp.dki_cnum ||
dkinfo.dki_unit != dkinfo_tmp.dki_unit) {
continue;
}
/*
* It's a mount on the disk we're checking. If we are
* checking whole disk, then we found trouble. We can
* quit searching.
*/
if (U_flag) {
if (!_dev_unmount(mp->mnt_special)) {
(void) fprintf(stderr,
gettext("%s: umount of %s failed\n"),
myname, mp->mnt_special);
found = 1;
}
} else {
(void) fprintf(stderr,
gettext("%s: %s is mounted (use -U flag)\n"),
myname, mp->mnt_special);
found = 1;
}
}
return (found);
}
static void
usage(char *str)
{
char *real_name, *alias_name;
if ((real_name = media_findname("floppy")) == NULL) {
if ((alias_name = _media_oldaliases("floppy")) != NULL)
real_name = media_findname(alias_name);
}
if (str[0] != ' ')
(void) printf("%s: %s\n", myname, str);
(void) printf(gettext(
/*CSTYLED*/
"\n usage: %s [-dDeEfHlLmMqUvx] [-b label] [-B file] [-t dostype] [devname]\n"),
myname);
(void) printf(gettext(
/*CSTYLED*/
" -b label install \"label\" on media\n"));
(void) printf(gettext(
" -B file install special boot loader on MS-DOS media\n"));
(void) printf(gettext(
/*CSTYLED*/
" -d format MS-DOS media\n"));
(void) printf(gettext(
/*CSTYLED*/
" -D format 720KB (3.5\") or 360KB (5.25\") Double-density diskette\n"));
(void) printf(gettext(
" -e eject the media when done\n"));
/*CSTYLED*/
(void) printf(gettext(
/*CSTYLED*/
" -E format 2.88MB (3.5\") Extended-density diskette\n"));
(void) printf(gettext(
" -f \"force\" - don't wait for confirmation\n"));
(void) printf(gettext(
/*CSTYLED*/
" -H format 1.44MB (3.5\") or 1.2MB (5.25\") High-density diskette\n"));
(void) printf(gettext(
/*CSTYLED*/
" -l format 720KB (3.5\") or 360KB (5.25\") Double-density diskette\n"));
(void) printf(gettext(
/*CSTYLED*/
" -L format 720KB (3.5\") or 360KB (5.25\") Double-density diskette\n"));
(void) printf(gettext(
" -m format 1.2MB (3.5\") Medium-density diskette\n"));
(void) printf(gettext(
" -M format 1.2MB (3.5\") Medium-density diskette\n"));
(void) printf(gettext(
" -q quiet\n"));
(void) printf(gettext(
/*CSTYLED*/
" -t dos format MS-DOS media (same as -d)\n"));
(void) printf(gettext(
" -t nec format NEC-DOS media (with -M only)\n"));
(void) printf(gettext(
/*CSTYLED*/
" -U unmount media if it's mounted\n"));
(void) printf(gettext(
" -v verify each block of the media\n"));
(void) printf(gettext(
" -x skip the format, only install SunOS or DOS label\n"));
(void) printf(gettext(
" devname defaults to '%s'\n"),
real_name ? real_name : gettext("no available default device"));
exit(1);
}
static int
verify(int fd, int blk, int len)
{
off_t off;
char *nullstring = "";
off = (off_t)(blk * (m_flag ? 1024 : 512));
if (lseek(fd, off, 0) != off) {
if (!q_flag)
(void) printf(gettext("e\n"));
(void) fprintf(stderr,
gettext("%s: can't seek to write verify, "), myname);
perror(nullstring);
return (4);
}
if (write(fd, ibuf1, len) != len) {
if (!q_flag)
(void) printf(gettext("e\n"));
if (blk == 0)
(void) fprintf(stderr,
gettext("%s: check diskette density, "),
myname);
else
(void) fprintf(stderr,
gettext("%s: can't write verify data, "),
myname);
perror(nullstring);
return (4);
}
if (lseek(fd, off, 0) != off) {
if (!q_flag)
(void) printf(gettext("e\n"));
(void) fprintf(stderr,
gettext("%s: bad seek to read verify, "),
myname);
perror(nullstring);
return (4);
}
if (read(fd, obuf, len) != len) {
if (!q_flag)
(void) printf(gettext("e\n"));
(void) fprintf(stderr,
gettext("%s: can't read verify data, "), myname);
perror(nullstring);
return (4);
}
if (memcmp(ibuf1, obuf, len)) {
if (!q_flag)
(void) printf(gettext("e\n"));
(void) fprintf(stderr, gettext("%s: verify data failure\n"),
myname);
return (4);
}
return (0);
}
/*
* write a SunOS label
* NOTE: this function assumes fd_vtoc has been filled in with the
* device specific information such as partition information
* and the asciilabel
*/
static void
write_SunOS_label(int fd, char *volname, struct vtoc *fd_vtoc)
{
char *nullstring = "";
fd_vtoc->v_sanity = VTOC_SANE;
/*
* The label structure is set up for DEV_BSIZE (512 byte) blocks,
* even though a medium density diskette has 1024 byte blocks
* See dklabel.h for more details.
*/
fd_vtoc->v_sectorsz = DEV_BSIZE;
(void) strncpy(fd_vtoc->v_volume, volname, sizeof (fd_vtoc->v_volume));
/* let the fd driver finish constructing the label and writing it */
if (ioctl(fd, DKIOCSVTOC, fd_vtoc) == -1) {
(void) fprintf(stderr,
gettext("%s: write of SunOS label failed, "), myname);
perror(nullstring);
exit(3);
}
}
/*
* MS-DOS Disk layout:
*
* ---------------------
* | Boot sector |
* |-------------------|
* | Reserved area |
* |-------------------|
* | FAT #1 |
* |-------------------|
* | FAT #2 |
* |-------------------|
* | Root directory |
* |-------------------|
* | |
* | File area |
* |___________________|
*/
/*
* The following is a copy of MS-DOS 3.3 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.
*
*/
static uchar_t bootsec[512] = {
0xeb, 0x34, 0x90, /* 8086 short jump + displacement + NOP */
'M', 'S', 'D', 'O', 'S', '3', '.', '3', /* OEM name & version */
0, 2, 1, 1, 0, /* Start of BIOS parameter block */
2, 224, 0, 0x40, 0xb, 0xf0, 9, 0,
18, 0, 2, 0, 0, 0, /* End of BIOS parameter block */
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x12,
0x0, 0x0, 0x0, 0x0,
0x1, 0x0, 0xfa, 0x33, /* 0x34, start of the bootstrap. */
0xc0, 0x8e, 0xd0, 0xbc, 0x0, 0x7c, 0x16, 0x7,
0xbb, 0x78, 0x0, 0x36, 0xc5, 0x37, 0x1e, 0x56,
0x16, 0x53, 0xbf, 0x2b, 0x7c, 0xb9, 0xb, 0x0,
0xfc, 0xac, 0x26, 0x80, 0x3d, 0x0, 0x74, 0x3,
0x26, 0x8a, 0x5, 0xaa, 0x8a, 0xc4, 0xe2, 0xf1,
0x6, 0x1f, 0x89, 0x47, 0x2, 0xc7, 0x7, 0x2b,
0x7c, 0xfb, 0xcd, 0x13, 0x72, 0x67, 0xa0, 0x10,
0x7c, 0x98, 0xf7, 0x26, 0x16, 0x7c, 0x3, 0x6,
0x1c, 0x7c, 0x3, 0x6, 0xe, 0x7c, 0xa3, 0x3f,
0x7c, 0xa3, 0x37, 0x7c, 0xb8, 0x20, 0x0, 0xf7,
0x26, 0x11, 0x7c, 0x8b, 0x1e, 0xb, 0x7c, 0x3,
0xc3, 0x48, 0xf7, 0xf3, 0x1, 0x6, 0x37, 0x7c,
0xbb, 0x0, 0x5, 0xa1, 0x3f, 0x7c, 0xe8, 0x9f,
0x0, 0xb8, 0x1, 0x2, 0xe8, 0xb3, 0x0, 0x72,
0x19, 0x8b, 0xfb, 0xb9, 0xb, 0x0, 0xbe, 0xd6,
0x7d, 0xf3, 0xa6, 0x75, 0xd, 0x8d, 0x7f, 0x20,
0xbe, 0xe1, 0x7d, 0xb9, 0xb, 0x0, 0xf3, 0xa6,
0x74, 0x18, 0xbe, 0x77, 0x7d, 0xe8, 0x6a, 0x0,
0x32, 0xe4, 0xcd, 0x16, 0x5e, 0x1f, 0x8f, 0x4,
0x8f, 0x44, 0x2, 0xcd, 0x19, 0xbe, 0xc0, 0x7d,
0xeb, 0xeb, 0xa1, 0x1c, 0x5, 0x33, 0xd2, 0xf7,
0x36, 0xb, 0x7c, 0xfe, 0xc0, 0xa2, 0x3c, 0x7c,
0xa1, 0x37, 0x7c, 0xa3, 0x3d, 0x7c, 0xbb, 0x0,
0x7, 0xa1, 0x37, 0x7c, 0xe8, 0x49, 0x0, 0xa1,
0x18, 0x7c, 0x2a, 0x6, 0x3b, 0x7c, 0x40, 0x38,
0x6, 0x3c, 0x7c, 0x73, 0x3, 0xa0, 0x3c, 0x7c,
0x50, 0xe8, 0x4e, 0x0, 0x58, 0x72, 0xc6, 0x28,
0x6, 0x3c, 0x7c, 0x74, 0xc, 0x1, 0x6, 0x37,
0x7c, 0xf7, 0x26, 0xb, 0x7c, 0x3, 0xd8, 0xeb,
0xd0, 0x8a, 0x2e, 0x15, 0x7c, 0x8a, 0x16, 0xfd,
0x7d, 0x8b, 0x1e, 0x3d, 0x7c, 0xea, 0x0, 0x0,
0x70, 0x0, 0xac, 0xa, 0xc0, 0x74, 0x22, 0xb4,
0xe, 0xbb, 0x7, 0x0, 0xcd, 0x10, 0xeb, 0xf2,
0x33, 0xd2, 0xf7, 0x36, 0x18, 0x7c, 0xfe, 0xc2,
0x88, 0x16, 0x3b, 0x7c, 0x33, 0xd2, 0xf7, 0x36,
0x1a, 0x7c, 0x88, 0x16, 0x2a, 0x7c, 0xa3, 0x39,
0x7c, 0xc3, 0xb4, 0x2, 0x8b, 0x16, 0x39, 0x7c,
0xb1, 0x6, 0xd2, 0xe6, 0xa, 0x36, 0x3b, 0x7c,
0x8b, 0xca, 0x86, 0xe9, 0x8a, 0x16, 0xfd, 0x7d,
0x8a, 0x36, 0x2a, 0x7c, 0xcd, 0x13, 0xc3, '\r',
'\n', 'N', 'o', 'n', '-', 'S', 'y', 's',
't', 'e', 'm', ' ', 'd', 'i', 's', 'k',
' ', 'o', 'r', ' ', 'd', 'i', 's', 'k',
' ', 'e', 'r', 'r', 'o', 'r', '\r', '\n',
'R', 'e', 'p', 'l', 'a', 'c', 'e', ' ',
'a', 'n', 'd', ' ', 's', 't', 'r', 'i',
'k', 'e', ' ', 'a', 'n', 'y', ' ', 'k',
'e', 'y', ' ', 'w', 'h', 'e', 'n', ' ',
'r', 'e', 'a', 'd', 'y', '\r', '\n', '\0',
'\r', '\n', 'D', 'i', 's', 'k', ' ', 'B',
'o', 'o', 't', ' ', 'f', 'a', 'i', 'l',
'u', 'r', 'e', '\r', '\n', '\0', 'I', 'O',
' ', ' ', ' ', ' ', ' ', ' ', 'S', 'Y',
'S', 'M', 'S', 'D', 'O', 'S', ' ', ' ',
' ', 'S', 'Y', 'S', '\0', 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0x55, 0xaa
};
static int
valid_DOS_boot(char *bootfile, uchar_t **bootloadp)
{
struct stat status;
size_t sizebootldr;
uchar_t *bootloader;
int bfd;
int boot_size = 0;
int err;
char *nullstring = "";
if ((err = stat(bootfile, &status)) != 0) {
(void) fprintf(stderr, gettext("%s: \"%s\" stat error %d\n"),
myname, bootfile, err);
return (0);
}
if ((boot_size = status.st_size) < 512) {
(void) fprintf(stderr,
gettext("%s: short boot sector"), myname);
perror(nullstring);
return (0);
}
sizebootldr = (boot_size + 511) / 512 * 512;
if ((bootloader = (uchar_t *)malloc((size_t)sizebootldr)) == NULL) {
(void) fprintf(stderr, gettext("%s: malloc error\n"),
myname);
return (0);
}
/* switch to user to access the boot file */
(void) seteuid(getuid());
if ((bfd = open(bootfile, O_RDONLY)) == -1) {
(void) fprintf(stderr, gettext("%s: could not open \"%s\": "),
myname, bootfile);
perror(nullstring);
return (0);
}
/* restore effective id */
(void) seteuid(euid);
if (read(bfd, bootloader, boot_size) != boot_size) {
(void) fprintf(stderr,
gettext("%s: read of MS-DOS boot file failed, "), myname);
perror(nullstring);
(void) close(bfd);
return (0);
}
if (!((*bootloader == 0xE9 ||
(*bootloader == 0xEB && *(bootloader + 2) == 0x90)) &&
*(bootloader + 510) == 0x55 &&
*(bootloader + 511) == 0xAA)) {
(void) fprintf(stderr,
gettext("%s: invalid MS-DOS boot loader image\n"), myname);
boot_size = 0;
}
(void) close(bfd);
*bootloadp = bootloader;
return (boot_size);
}
static void
write_DOS_label(int fd, uchar_t *bootloadr, int bootlen, char *altbootname,
char *doslabel, struct bios_param_blk *bpb, int rdirsec)
{
int i, j;
int bootclen;
size_t fat_bsize;
ushort_t totalsec;
uchar_t *fat_rdir;
uchar_t *fatptr;
char *nullstring = "";
if (bootlen < 512 || !bootloadr) {
/* use default boot loader routine */
bootloadr = bootsec;
bootlen = 512;
} else
(void) printf
(gettext("%s: using \"%s\" for MS-DOS boot loader\n"),
myname, altbootname);
if (bootlen % 512 > 0)
bootlen = (bootlen + 511) / 512 * 512;
bpb->b_bps[0] = getlobyte(512);
bpb->b_bps[1] = gethibyte(512);
/* MS-DOS 5.0 supports only 1 reserved sector :-( */
bpb->b_res_sec[0] = 1;
bpb->b_res_sec[1] = 0;
totalsec = fdchar.fdc_ncyl * fdchar.fdc_nhead * fdchar.fdc_secptrack;
bpb->b_totalsec[0] = getlobyte(totalsec);
bpb->b_totalsec[1] = gethibyte(totalsec);
bpb->b_spt[0] = fdchar.fdc_secptrack;
bpb->b_spt[1] = 0;
bpb->b_nhead[0] = fdchar.fdc_nhead;
bpb->b_nhead[1] = 0;
bpb->b_hiddensec[0] = 0;
bpb->b_hiddensec[1] = 0;
bpb->b_rdirents[0] = getlobyte(rdirsec);
bpb->b_rdirents[1] = gethibyte(rdirsec);
(void) memcpy((char *)(bootloadr + 0x0B), (char *)bpb,
sizeof (struct bios_param_blk));
if (write(fd, bootloadr, 512) != 512) {
(void) fprintf(stderr,
gettext("%s: write of MS-DOS boot sector failed"), myname);
perror(nullstring);
exit(3);
}
bootloadr += 512;
bootlen -= 512;
fat_bsize = 512 * bpb->b_fatsec[0];
fat_rdir = (uchar_t *)malloc(fat_bsize);
(void) memset(fat_rdir, (char)0, fat_bsize);
*fat_rdir = bpb->b_mediadescriptor;
*(fat_rdir + 1) = 0xFF;
*(fat_rdir + 2) = 0xFF;
bootclen = (bootlen + 512 * (int)bpb->b_spcl - 1) /
(512 * (int)bpb->b_spcl);
#define BAD_CLUSTER 0xFF7
for (i = 0, fatptr = fat_rdir+3; i < bootclen; i++)
/*
* pre-allocate any clusters used by boot loader if
* loader will occupy more than 1 sector
*/
if (!(i & 01)) {
*fatptr++ = BAD_CLUSTER & 0xFF;
*fatptr = (BAD_CLUSTER >> 8) & 0x0F;
} else {
*fatptr = (*fatptr & 0x0F) |
((BAD_CLUSTER << 4) & 0xF0);
fatptr++;
*fatptr++ = (BAD_CLUSTER >> 4) & 0xFF;
}
for (i = 0; i < (int)bpb->b_nfat; ++i)
if (write(fd, fat_rdir, fat_bsize) != fat_bsize) {
(void) fprintf(stderr,
gettext("%s: write of MS-DOS File Allocation Table failed, "),
myname);
perror(nullstring);
exit(3);
}
rdirsec = bpb->b_rdirents[0];
rdirsec = 32 * (int)rdirsec / 512;
if (b_flag) {
struct timeval tv;
struct tm *tp;
ushort_t dostime;
ushort_t dosday;
/* the label can be no more than 11 characters */
j = min(11, (int)strlen(doslabel));
for (i = 0; i < j; i++) {
fat_rdir[i] = uppercase(doslabel[i]);
}
for (; i < 11; i++) {
fat_rdir[i] = ' ';
}
fat_rdir[0x0B] = 0x28;
(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;
fat_rdir[0x16] = getlobyte(dostime);
fat_rdir[0x17] = gethibyte(dostime);
fat_rdir[0x18] = getlobyte(dosday);
fat_rdir[0x19] = gethibyte(dosday);
if (write(fd, fat_rdir, 512) != 512) {
(void) fprintf(stderr,
gettext("%s: write of MS-DOS FAT failed, "),
myname);
perror(nullstring);
exit(3);
}
i = 1;
} else {
i = 0;
}
(void) memset(fat_rdir, (char)0, 512);
for (; i < (int)rdirsec; ++i) {
if (write(fd, fat_rdir, 512) != 512) {
(void) fprintf(stderr,
gettext("%s: write of MS-DOS root directory failed, "),
myname);
perror(nullstring);
exit(3);
}
}
/*
* Write the rest of the boot loader if it's longer than one sector.
* The clusters used are marked Bad in the FAT.
* No directory entry exists for this file (so that it cannot be
* deleted).
*/
if (bootlen && write(fd, bootloadr, bootlen) != bootlen) {
(void) fprintf(stderr,
gettext("%s: write of MS-DOS boot sectors failed"), myname);
perror(nullstring);
exit(3);
}
}
static void
write_NEC_DOS_label(int fd, char *doslabel)
{
struct bios_param_blk *bpb;
ushort_t fatsec;
ushort_t rdirsec;
char fat_rdir[1024];
int i, j, m = 1;
uchar_t bootsec_NEC[1024];
char *nullstring = "";
uchar_t bios_param_NEC[30] = { 0xeb, 0x1c, 0x90, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x1, 0x1, 0x0,
0x2, 0xc0, 0x0, 0xd0, 0x4, 0xfe, 0x2, 0x0,
0x8, 0x0, 0x2, 0x0, 0x0, 0x0
};
uchar_t fatdir[32] = { 0xe5, 0xe5, 0xe5, 0xe5, 0xe5, 0xe5, 0xe5, 0xe5,
0xe5, 0xe5, 0xe5, 0xe5, 0xe5, 0xe5, 0xe5, 0xe5,
0xe5, 0xe5, 0xe5, 0xe5, 0xe5, 0xe5, 0xe5, 0xe5,
0xe5, 0xe5, 0xe5, 0xe5, 0xe5, 0xe5, 0xe5, 0xe5
};
(void) memset(bootsec_NEC, (char)0, 1024);
(void) memcpy(&bootsec_NEC, &bios_param_NEC, 30);
bpb = (struct bios_param_blk *)&(bootsec_NEC[0xb]);
if (write(fd, &bootsec_NEC[0], 1024) != 1024) {
(void) fprintf(stderr, gettext(
"%s: write of NEC-DOS boot sector failed, "),
myname);
perror(nullstring);
exit(3);
}
(void) memset(fat_rdir, (char)0, 1024);
fatsec = bpb->b_fatsec[0];
for (i = 0; i < (int)bpb->b_nfat * (int)fatsec; ++i) {
if ((i % (int)fatsec) == 0) {
fat_rdir[0] = bpb->b_mediadescriptor;
fat_rdir[1] = (char)0xff;
fat_rdir[2] = (char)0xff;
fat_rdir[3] = 0;
fat_rdir[4] = 0;
fat_rdir[5] = 0;
} else {
fat_rdir[0] = 0;
fat_rdir[1] = 0;
fat_rdir[2] = 0;
fat_rdir[3] = 0;
fat_rdir[4] = 0;
fat_rdir[5] = 0;
}
if (write(fd, &fat_rdir[0], 1024) != 1024) {
(void) fprintf(stderr,
/*CSTYLED*/
gettext("%s: write of NEC-DOS File Allocation Table failed, "), myname);
perror(nullstring);
exit(3);
}
}
#ifndef sparc
/* LINTED */
rdirsec = (int)htols(bpb->b_rdirents[0]) * 32 /1024;
#else
rdirsec = (int)htols(bpb->b_rdirents[0]) * 32 /1024;
#endif
if (b_flag) {
struct timeval tv;
struct tm *tp;
ushort_t dostime;
ushort_t dosday;
/* the label can be no more than 11 characters */
j = min(11, (int)strlen(doslabel));
for (i = 0; i < j; i++) {
fat_rdir[i] = uppercase(doslabel[i]);
}
for (; i < 11; i++) {
fat_rdir[i] = ' ';
}
fat_rdir[0xb] = 0x28;
(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;
fat_rdir[0x16] = getlobyte(dostime);
fat_rdir[0x17] = gethibyte(dostime);
fat_rdir[0x18] = getlobyte(dosday);
fat_rdir[0x19] = gethibyte(dosday);
if (write(fd, &fat_rdir[0], 1024) != 1024) {
(void) fprintf(stderr,
/*CSTYLED*/
gettext("%s: write of NEC-DOS root directory failed, "), myname);
perror(nullstring);
exit(3);
}
(void) memset(fat_rdir, (char)0, 512);
i = 1;
} else {
i = 0;
while (m < 1024) {
(void) memcpy(&fat_rdir[m], &fatdir, 31);
m = m + 32;
}
}
for (; i < (int)rdirsec; ++i) {
if (write(fd, &fat_rdir[0], 1024) != 1024) {
(void) fprintf(stderr,
/*CSTYLED*/
gettext("%s: write of NEC-DOS root directory failed, "), myname);
perror(nullstring);
exit(3);
}
}
}
|