summaryrefslogtreecommitdiffstats
path: root/private/ntos/fw/alpha/jxfboot.c
blob: 039b56ee7f18e45a1c4b83ec57efc2448bbde5f0 (plain) (blame)
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
/*++

Copyright (c) 1989  Microsoft Corporation
Copyright (c) 1993  Digital Equipment Corporation

Module Name:

    jxfboot.c

Abstract:

    This module implements the floppy disk boot driver for the Jazz system.

Author:

    Darryl E. Havens (darrylh) 28-Aug-1989

Environment:

    Kernel mode only, raised IRQL, generally self-contained.


Revision History:

    31-August-1992	John DeRosa [DEC]

    Made Alpha/Jensen modifications.  This file is now Alpha-specific.

--*/

#include "fwp.h"
#include "jnsnprom.h"
#include "ntdddisk.h"
#include "flo_data.h"
#include "xxstring.h"

//
// Define local static data.
//

UCHAR DebugByte[8];
ULONG MotorStatus;
PDRIVE_MEDIA_CONSTANTS CurrentDriveMediaConstants;

//
// Define timeout constants.
//

#define MICROSECONDS_10                 10
#define MICROSECONDS_20                 20
#define MICROSECONDS_250                250
#define MICROSECONDS_500                500
#define MILLISECONDS_15                 (15 * 1000)
#define MILLISECONDS_30                 (30 * 1000)
#define MILLISECONDS_500                (500 * 1000)
#define SECONDS_2                       (2 * 1000 * 1000)
#define FW_FLOPPY_TIMEOUT               2

//
// Define the number of times an operation is retried before it is considered
// to be a hard error.
//

#define RETRY_COUNT  8

//
// Define the MINIMUM macro.
//

#define MINIMUM( x, y ) ( x <= y ? x : y )

//
// Define floppy device register structure.
//

typedef struct _FLOPPY_REGISTERS {
    UCHAR StatusRegisterA;
    UCHAR StatusRegisterB;
    UCHAR DigitalOutput;
    UCHAR Reserved1;
    union {
        UCHAR MainStatus;
        UCHAR DataRateSelect;
    } MsrDsr;
    UCHAR Fifo;
    UCHAR Reserved2;
    union {
        UCHAR DigitalInput;
        UCHAR ConfigurationControl;
    } DirCcr;
} FLOPPY_REGISTERS, *PFLOPPY_REGISTERS;

//
// Define pointer to the floppy registers.
//

#define FLOPPY_CONTROL ((volatile PFLOPPY_REGISTERS)FLOPPY_VIRTUAL_BASE)



//
// Define bits in some of the floppy registers.  The existing Microsoft
// code uses constants (e.g. 0xC0) for register masking.  These defines
// will be used in Digital Equipment Corporation code.
//

#define	FLOPPY_SRA_INTPENDING	0x80
#define	FLOPPY_MSR_DRV0BUSY	0x1
#define	FLOPPY_MSR_DRV1BUSY	0x2
#define	FLOPPY_MSR_CMDBUSY	0x10
#define	FLOPPY_MSR_NONDMA	0x20
#define	FLOPPY_MSR_DIO		0x40
#define	FLOPPY_MSR_RQM		0x80

ARC_STATUS
FloppyClose (
    IN ULONG FileId
    );

ARC_STATUS
FloppyMount (
    IN PCHAR MountPath,
    IN MOUNT_OPERATION Operation
    );

ARC_STATUS
FloppyOpen (
    IN PCHAR OpenPath,
    IN OPEN_MODE OpenMode,
    OUT PULONG FileId
    );

ARC_STATUS
FloppyRead (
    IN ULONG FileId,
    IN PVOID Buffer,
    IN ULONG Length,
    OUT PULONG Count
    );

ARC_STATUS
FloppyGetReadStatus (
    IN ULONG FileId
    );

ARC_STATUS
FloppySeek (
    IN ULONG FileId,
    IN PLARGE_INTEGER Offset,
    IN SEEK_MODE SeekMode
    );

ARC_STATUS
FloppyWrite (
    IN ULONG FileId,
    IN PVOID Buffer,
    IN ULONG Length,
    OUT PULONG Count
    );

ARC_STATUS
FloppyGetFileInformation (
    IN ULONG FileId,
    OUT PFILE_INFORMATION Finfo
    );

ARC_STATUS
FloppyBootIO(
    IN PMDL MdlAddress,
    IN ULONG StartingBlock,
    IN ULONG FileId,
    IN BOOLEAN ReadWrite
    );

VOID
ClearFloppyFifo (
    IN VOID
    );

ULONG
ReadFloppyFifo (
    IN PUCHAR Buffer
    );

VOID
WriteFloppyFifo(
    IN PUCHAR Buffer,
    IN ULONG Size
    );

//
// Declare and Initialize the floppy disk device entry table.
//

BL_DEVICE_ENTRY_TABLE FloppyEntryTable = {
    FloppyClose,
    FloppyMount,
    FloppyOpen,
    FloppyRead,
    FloppyGetReadStatus,
    FloppySeek,
    FloppyWrite,
    FloppyGetFileInformation,
    (PARC_SET_FILE_INFO_ROUTINE)NULL
    };

//
// Define prototypes for all routines used by this module.
//


ARC_STATUS
WaitForFloppyInterrupt(
	ULONG Timeout
	);

ARC_STATUS
FloppyBootClose(
    );

BOOLEAN
Recalibrate (
    UCHAR   DriveNumber
    );

VOID
FloppyBootSetup(
    VOID
    );

UCHAR
ReceiveByte (
    );

BOOLEAN
SendByte(
    IN UCHAR SourceByte
    );

ARC_STATUS
FloppyDetermineMediaType(
    IN OUT PFLOPPY_CONTEXT FloppyContext,
    OUT PMEDIA_TYPE mediaType
    );

ARC_STATUS
FloppyDatarateSpecifyConfigure(
    IN DRIVE_MEDIA_TYPE DriveMediaType,
    IN UCHAR DriveNumber
    );


ARC_STATUS
FloppyClose (
    IN ULONG FileId
    )

/*++

Routine Description:

    This function closes the file table entry specified by the file id.

Arguments:

    FileId - Supplies the file table index.

Return Value:

    ESUCCESS is returned

--*/

{
    FloppyBootClose();
    BlFileTable[FileId].Flags.Open = 0;
    return ESUCCESS;
}

ARC_STATUS
FloppyMount (
    IN PCHAR MountPath,
    IN MOUNT_OPERATION Operation
    )

/*++

Routine Description:


Arguments:


Return Value:


--*/

{
    return ESUCCESS;
}

ARC_STATUS
FloppyOpen (
    IN PCHAR OpenPath,
    IN OPEN_MODE OpenMode,
    IN OUT PULONG FileId
    )

/*++

Routine Description:


Arguments:


Return Value:


--*/

{
    PCONFIGURATION_COMPONENT FloppyComponent, FloppyController;
    UCHAR Data[sizeof(CM_PARTIAL_RESOURCE_LIST) +
               sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) * 8 +
               sizeof(CM_FLOPPY_DEVICE_DATA)];
    PCM_PARTIAL_RESOURCE_LIST List = (PCM_PARTIAL_RESOURCE_LIST)Data;
    PCM_FLOPPY_DEVICE_DATA FloppyData;
    ULONG DriveNumber;
    ULONG Index;
    ARC_STATUS ArcStatus;
    CHAR TempBuffer[SECTOR_SIZE + 32];
    PCHAR TempPointer;
    ULONG Count;
    UCHAR mediaDescriptor;
    MEDIA_TYPE mediaType = Unknown;
    DRIVE_MEDIA_TYPE driveMediaType;
    ULONG DriveType;
    ULONG ConfigDriveType;

    //
    // Get the drive number from the pathname.
    //

    if (JzGetPathMnemonicKey(OpenPath, "fdisk", &DriveNumber)) {
        return ENODEV;
    }

    //
    // If the ARC CDS data is invalid, default to 2.88MB floppy
    //

    BlFileTable[*FileId].u.FloppyContext.DriveType = DRIVE_TYPE_2880;

    //
    // Look in the configuration database for the floppy device data to
    // determine the size of the floppy drive.
    //

    FloppyComponent = FwGetComponent(OpenPath);
    if ((FloppyComponent != NULL) &&
        (FloppyComponent->Type == FloppyDiskPeripheral)) {
        if (FwGetConfigurationData(List, FloppyComponent) == ESUCCESS) {
            FloppyData = (PCM_FLOPPY_DEVICE_DATA)&List->PartialDescriptors[List->Count];
            if (strcmp(FloppyData->Size,"5.25")==0) {
                BlFileTable[*FileId].u.FloppyContext.DriveType = DRIVE_TYPE_1200;
            } else {
                if (strcmp(FloppyData->Size,"3.5")==0) {
                    if (FloppyData->MaxDensity == 2880) {
                        BlFileTable[*FileId].u.FloppyContext.DriveType = DRIVE_TYPE_2880;
                    } else {
                        BlFileTable[*FileId].u.FloppyContext.DriveType = DRIVE_TYPE_1440;
                    }
                }
            }
        }
    }

    ConfigDriveType = BlFileTable[*FileId].u.FloppyContext.DriveType;
    BlFileTable[*FileId].u.FloppyContext.DiskId = DriveNumber;
    BlFileTable[*FileId].Position.LowPart=0;
    BlFileTable[*FileId].Position.HighPart=0;

    //
    // Enable the drive and start the motor via the DOR.
    //

    if (MotorStatus != DriveNumber) {
        WRITE_PORT_UCHAR((PUCHAR)&FLOPPY_CONTROL->DigitalOutput,
                             ((0xc + DriveNumber) + (1 << (DriveNumber + 4))));
        MotorStatus = DriveNumber;

        //
        // Wait for at least 500ms to ensure that the motor really is running.
        //

        FwStallExecution(MILLISECONDS_500);
    }

    //
    // Determine the disk density.
    //

    ClearFloppyFifo();
    ArcStatus = FloppyDetermineMediaType(&BlFileTable[*FileId].u.FloppyContext,
					 &mediaType);
    if (ArcStatus == EIO) {

        FloppyClose(*FileId);

        //
        // Reset the floppy, it seems to get in a bad state.
        //

        FloppyBootSetup();
        return(ArcStatus);

    } else if (ArcStatus != ESUCCESS) {

        //
        // The floppy was not readable, so try next lowest floppy type.
        //

        DriveType = BlFileTable[*FileId].u.FloppyContext.DriveType;

        if (DriveType == DRIVE_TYPE_2880) {
            BlFileTable[*FileId].u.FloppyContext.DriveType = DRIVE_TYPE_1440;
        } else if (DriveType == DRIVE_TYPE_1440) {
            BlFileTable[*FileId].u.FloppyContext.DriveType = DRIVE_TYPE_1200;
        }

        ArcStatus = FloppyDetermineMediaType(&BlFileTable[*FileId].u.FloppyContext,
					     &mediaType);

        if (ArcStatus != ESUCCESS) {
//            FwPrint("Unrecognized floppy format\r\n");
            FloppyClose(*FileId);
            return(ArcStatus);
        }
    }

    //
    // Read the first sector to get the media descriptor byte.
    //

    TempPointer =  (PVOID) ((ULONG) (TempBuffer + KeGetDcacheFillSize() - 1)
			    & ~(KeGetDcacheFillSize() - 1));
    ArcStatus = FloppyRead(*FileId, TempPointer, SECTOR_SIZE, &Count);

    if (ArcStatus != ESUCCESS) {
//        FwPrint("Error opening floppy\r\n");
        FloppyClose(*FileId);
        return(ArcStatus);
    }

    //
    // Check the media descriptor byte to verify that we have the right
    // drive and media type.
    //

    DriveType = BlFileTable[*FileId].u.FloppyContext.DriveType;
    mediaDescriptor = *( TempPointer + MEDIA_DESCRIPTOR_OFFSET );

    switch ( mediaDescriptor ) {

    case MEDIA_DESCRIPTOR_160K:
        mediaType = F5_160_512;
        DriveType = DRIVE_TYPE_1200;
        break;

    case MEDIA_DESCRIPTOR_180K:
        mediaType = F5_180_512;
        DriveType = DRIVE_TYPE_1200;
        break;

    case MEDIA_DESCRIPTOR_320K:
        mediaType = F5_320_512;
        DriveType = DRIVE_TYPE_1200;
        break;

    case MEDIA_DESCRIPTOR_360K:
        mediaType = F5_360_512;
        DriveType = DRIVE_TYPE_1200;
        break;

    case MEDIA_DESCRIPTOR_720K_OR_1220K:

        //
        // The following code tries to take care of the case when the floppy
        // is really a 5 1/4" drive but the firmware thinks its 3 1/2".  A
        // 1.2 MByte floppy can be read with the 1.44 MByte parameters, but
        // the descriptor byte will be MEDIA_DESCRIPTOR_720K_OR_1220K.  Check
        // if the parameters are really for 720 K, otherwise default to
        // 1.2 MByte.
        //

        if ((DriveType == DRIVE_TYPE_1440) &&
            (BlFileTable[*FileId].u.FloppyContext.SectorsPerTrack == 9)) {
            mediaType = F3_720_512;
        } else {
            mediaType = F5_1Pt2_512;
            DriveType = DRIVE_TYPE_1200;
        }
        break;


    case MEDIA_DESCRIPTOR_1440K_OR_2880K:
    default:

	//
	// FloppyDetermineMediaType did the right thing for this case,
	// so no additional verification is needed.
	//

        break;
    }


    if ( mediaType != 0 ) {

        //
        // Find the constants for this media type.
        //

        driveMediaType = DriveMediaLimits[DriveType].HighestDriveMediaType;
        while ( ( DriveMediaConstants[driveMediaType].MediaType != mediaType ) &&
                ( driveMediaType > DriveMediaLimits[DriveType].LowestDriveMediaType ) ) {

            driveMediaType--;
        }

        //
        // Set the sectors per track and the drive type in the floppy
        // context record.
        //

        BlFileTable[*FileId].u.FloppyContext.SectorsPerTrack =
                DriveMediaConstants[driveMediaType].SectorsPerTrack;
        BlFileTable[*FileId].u.FloppyContext.DriveType = DriveType;
    }

    //
    // If the floppy drive type has changed, update the configuration database
    // with the correct drive type.
    //

    if (DriveType != ConfigDriveType) {

	switch (DriveType) {

	  case DRIVE_TYPE_1200:

            strcpy(FloppyData->Size,"5.25");
            FloppyData->MaxDensity = 1200;
	    break;

	  case DRIVE_TYPE_1440:

            strcpy(FloppyData->Size,"3.5");
            FloppyData->MaxDensity = 1440;
	    break;

	  default:

            strcpy(FloppyData->Size,"3.5");
            FloppyData->MaxDensity = 2880;
	    break;
        }

        //
        // Get a pointer to the floppy controller component.
        //

        if ((FloppyController = FwGetParent(FloppyComponent)) != NULL) {

            //
            // Delete the old entry, note that this does not actually delete the
            // data in the database, it only changes the pointers, so that the
            // AddChild call can still use the old component data structure.
            //

            if (FwDeleteComponent(FloppyComponent) == ESUCCESS) {

                //
                // Add back the modified floppy structure.
                //

                FwAddChild(FloppyController, FloppyComponent, List);
            }
        }
    }

    return ESUCCESS;
}

ARC_STATUS
FloppyRead (
    IN ULONG FileId,
    IN PVOID Buffer,
    IN ULONG Length,
    OUT PULONG Count
    )

/*++

Routine Description:

    This function reads data from the floppy starting at the position
    specified in the file table.


Arguments:

    FileId - Supplies the file table index.

    Buffer - Supplies a poiner to the buffer that receives the data
        read.

    Length - Supplies the number of bytes to be read.

    Count - Supplies a pointer to a variable that receives the number of
        bytes actually read.

Return Value:


    The read completion status is returned.

--*/

{

    ARC_STATUS ArcStatus;
    ULONG FrameNumber;
    ULONG Index;
    ULONG Limit;
    PMDL MdlAddress;
    UCHAR MdlBuffer[sizeof(MDL) + ((64 / 4) + 1) * sizeof(ULONG)];
    ULONG NumberOfPages;
    ULONG Offset;
    PULONG PageFrame;
    ULONG Position;
    CHAR TempBuffer[SECTOR_SIZE + 32];
    PCHAR TempPointer;

    //
    // If the requested size of the transfer is zero return ESUCCESS
    //
    if (Length==0) {
        return ESUCCESS;
    }
    //
    // If the current position is not at a sector boundary , then
    // read the first and/or last sector separately and copy the data.
    //

    Offset = BlFileTable[FileId].Position.LowPart & (SECTOR_SIZE - 1);
    if (Offset != 0) {
        //
        // Adjust position to the sector boundary, align the transfer address
        // and read that first sector.
        //
        BlFileTable[FileId].Position.LowPart -= Offset;
        TempPointer =  (PVOID) ((ULONG) (TempBuffer + KeGetDcacheFillSize() - 1)
            & ~(KeGetDcacheFillSize() - 1));

        ArcStatus = FloppyRead(FileId, TempPointer, SECTOR_SIZE, Count);

        //
        // If the transfer was not successful, then reset the position
        // and return the completion status.
        //
        if (ArcStatus != ESUCCESS) {
            BlFileTable[FileId].Position.LowPart += Offset;
            return ArcStatus;
        }

        //
        // If the length of read is less than the number of bytes from
        // the offset to the end of the sector, then copy only the number
        // of bytes required to fulfil the request. Otherwise copy to the end
        // of the sector and, read the remaining data.
        //

        if ((SECTOR_SIZE - Offset) > Length) {
            Limit = Offset + Length;
        } else {
            Limit = SECTOR_SIZE;
        }

        //
        // Copy the data to the specified buffer.
        //
        for (Index = Offset; Index < Limit; Index += 1) {
            *((PCHAR)Buffer)++ = *(TempPointer + Index);
        }

        //
        // Adjust the current position and
        // Read the remaining part of the specified transfer.
        //

        BlFileTable[FileId].Position.LowPart -= SECTOR_SIZE-Limit;
        Position = BlFileTable[FileId].Position.LowPart;
        ArcStatus = FloppyRead(FileId,
                               Buffer,
                               Length - (Limit - Offset),
                               Count);

        //
        // If the transfer was not successful, then reset the device
        // position and return the completion status.
        //

        if (ArcStatus != ESUCCESS) {
            BlFileTable[FileId].Position.LowPart = Position;
            return ArcStatus;
        } else {
            *Count = Length;
            return ESUCCESS;
        }
    } else {
        //
        // if the size of requested data is not a multiple of the sector
        // size then read the last sector separately.
        //
        if (Length & (SECTOR_SIZE - 1)) {
            Position = BlFileTable[FileId].Position.LowPart;
            ArcStatus = FloppyRead(FileId,
                                   Buffer,
                                   Length & (~(SECTOR_SIZE - 1)),
                                   Count);

            //
            // If the transfer was not successful, then reset the device
            // position and return the completion status.
            //
            if (ArcStatus != ESUCCESS) {
                BlFileTable[FileId].Position.LowPart = Position;
                return ArcStatus;
            }

            //
            // Read the last sector and copy the requested data.
            //
            TempPointer =  (PVOID) ((ULONG) (TempBuffer + KeGetDcacheFillSize() - 1)
                & ~(KeGetDcacheFillSize() - 1));

            ArcStatus = FloppyRead(FileId, TempPointer, SECTOR_SIZE, Count);

            //
            // If the transfer was not successful return the completion status.
            //
            if (ArcStatus != ESUCCESS) {
                return ArcStatus;
            }

            //
            // Copy the data to the specified buffer.
            //
            (PCHAR)Buffer += Length & (~(SECTOR_SIZE - 1));
            Limit =  Length & (SECTOR_SIZE - 1);
            for (Index = 0; Index < Limit; Index += 1) {
                *((PCHAR)Buffer)++ = *(TempPointer + Index);
            }
            BlFileTable[FileId].Position.LowPart -= SECTOR_SIZE - Limit;
            *Count = Length;
            return ESUCCESS;



        } else {

            //
            // Build the memory descriptor list.
            //

            MdlAddress = (PMDL)&MdlBuffer[0];
            MdlAddress->Next = NULL;
            MdlAddress->Size = sizeof(MDL) +
                  ADDRESS_AND_SIZE_TO_SPAN_PAGES(Buffer, Length) * sizeof(ULONG);
            MdlAddress->StartVa = (PVOID)PAGE_ALIGN(Buffer);
            MdlAddress->ByteCount = Length;
            MdlAddress->ByteOffset = BYTE_OFFSET(Buffer);
            PageFrame = (PULONG)(MdlAddress + 1);
            FrameNumber = (((ULONG)MdlAddress->StartVa) & 0x1fffffff) >> PAGE_SHIFT;
            NumberOfPages = (MdlAddress->ByteCount +
                  MdlAddress->ByteOffset + PAGE_SIZE - 1) >> PAGE_SHIFT;
            for (Index = 0; Index < NumberOfPages; Index += 1) {
                *PageFrame++ = FrameNumber++;
            }

            //
            // Flush I/O buffers and read from the boot device.
            //

            HalFlushIoBuffers(MdlAddress, TRUE, TRUE);
            ArcStatus = FloppyBootIO(MdlAddress,
                                     BlFileTable[FileId].Position.LowPart >> SECTOR_SHIFT,
                                     FileId,
                                     FALSE);

            if (ArcStatus == ESUCCESS) {
                BlFileTable[FileId].Position.LowPart += Length;
                *Count = Length;
                return ESUCCESS;
            } else {
                *Count = 0;
                return EIO;
            }
        }
    }
}

ARC_STATUS
FloppyWrite (
    IN ULONG FileId,
    IN PVOID Buffer,
    IN ULONG Length,
    OUT PULONG Count
    )

/*++

Routine Description:

    This function writes data to the floppy starting at the position
    specified in the file table.


Arguments:

    FileId - Supplies the file table index.

    Buffer - Supplies a poiner to the buffer that contains the data
        to be written.

    Length - Supplies the number of bytes to be writtes.

    Count - Supplies a pointer to a variable that receives the number of
        bytes actually written.

Return Value:


    The write completion status is returned.

--*/

{

    ARC_STATUS ArcStatus;
    ULONG FrameNumber;
    ULONG Index;
    ULONG Limit;
    PMDL MdlAddress;
    UCHAR MdlBuffer[sizeof(MDL) + ((64 / 4) + 1) * sizeof(ULONG)];
    ULONG NumberOfPages;
    ULONG Offset;
    PULONG PageFrame;
    ULONG Position;
    CHAR TempBuffer[SECTOR_SIZE + 32];
    PCHAR TempPointer;

    //
    // If the requested size of the transfer is zero return ESUCCESS
    //
    if (Length==0) {
        return ESUCCESS;
    }
    //
    // If the current position is not at a sector boundary , then
    // read the first and/or last sector separately and copy the data.
    //

    Offset = BlFileTable[FileId].Position.LowPart & (SECTOR_SIZE - 1);
    if (Offset != 0) {
        //
        // Adjust position to the sector boundary, align the transfer address
        // and read that first sector.
        //
        BlFileTable[FileId].Position.LowPart -= Offset;
        TempPointer =  (PVOID) ((ULONG) (TempBuffer + KeGetDcacheFillSize() - 1)
            & ~(KeGetDcacheFillSize() - 1));

        ArcStatus = FloppyRead(FileId, TempPointer, SECTOR_SIZE, Count);

        //
        // If the transfer was not successful, then reset the position
        // and return the completion status.
        //
        if (ArcStatus != ESUCCESS) {
            BlFileTable[FileId].Position.LowPart += Offset;
            return ArcStatus;
        } else {
            //
            // Reset the position as it was before the read.
            //
            BlFileTable[FileId].Position.LowPart -= SECTOR_SIZE;
        }

        //
        // If the length of write is less than the number of bytes from
        // the offset to the end of the sector, then copy only the number
        // of bytes required to fulfil the request. Otherwise copy to the end
        // of the sector and, read the remaining data.
        //

        if ((SECTOR_SIZE - Offset) > Length) {
            Limit = Offset + Length;
        } else {
            Limit = SECTOR_SIZE;
        }

        //
        // Merge the data from the specified buffer.
        //
        for (Index = Offset; Index < Limit; Index += 1) {
            *(TempPointer + Index) = *((PCHAR)Buffer)++;
        }

        //
        // Write the modified sector.
        //
        ArcStatus = FloppyWrite(FileId, TempPointer, SECTOR_SIZE, Count);

        if (ArcStatus != ESUCCESS) {
            return ArcStatus;
        }

        //
        // Adjust the current position and
        // Write the remaining part of the specified transfer.
        //

        BlFileTable[FileId].Position.LowPart -= SECTOR_SIZE-Limit;
        Position = BlFileTable[FileId].Position.LowPart;
        ArcStatus = FloppyWrite(FileId,
                               Buffer,
                               Length - (Limit - Offset),
                               Count);

        //
        // If the transfer was not successful, then reset the device
        // position and return the completion status.
        //

        if (ArcStatus != ESUCCESS) {
            BlFileTable[FileId].Position.LowPart = Position;
            return ArcStatus;
        } else {
            *Count = Length;
            return ESUCCESS;
        }
    } else {
        //
        // if the size of requested data is not a multiple of the sector
        // size then write the last sector separately.
        //
        if (Length & (SECTOR_SIZE - 1)) {

            //
            // Do the transfer of the complete sectors in the middle
            //
            Position = BlFileTable[FileId].Position.LowPart;
            ArcStatus = FloppyWrite(FileId,
                                   Buffer,
                                   Length & (~(SECTOR_SIZE - 1)),
                                   Count);

            //
            // If the transfer was not successful, then reset the device
            // position and return the completion status.
            //
            if (ArcStatus != ESUCCESS) {
                BlFileTable[FileId].Position.LowPart = Position;
                return ArcStatus;
            }

            //
            // Read the last sector and copy the requested data.
            //
            TempPointer =  (PVOID) ((ULONG) (TempBuffer + KeGetDcacheFillSize() - 1)
                & ~(KeGetDcacheFillSize() - 1));

            ArcStatus = FloppyRead(FileId, TempPointer, SECTOR_SIZE, Count);

            //
            // If the transfer was not successful return the completion status.
            //
            if (ArcStatus != ESUCCESS) {
                return ArcStatus;
            }

            //
            // Copy the data to the specified buffer.
            //
            (PCHAR)Buffer += Length & (~(SECTOR_SIZE - 1));
            Limit =  Length & (SECTOR_SIZE - 1);
            for (Index = 0; Index < Limit; Index += 1) {
                *(TempPointer + Index) = *((PCHAR)Buffer)++;
            }
            //
            // Adjust the position and write the data.
            //
            BlFileTable[FileId].Position.LowPart -= SECTOR_SIZE;
            ArcStatus = FloppyWrite(FileId, TempPointer, SECTOR_SIZE, Count);

            //
            // Set the position for the requested transfer
            //
            BlFileTable[FileId].Position.LowPart -= SECTOR_SIZE - Limit;

            *Count = Length;
            return ArcStatus;

        } else {

            //
            // Build the memory descriptor list.
            //

            MdlAddress = (PMDL)&MdlBuffer[0];
            MdlAddress->Next = NULL;
            MdlAddress->Size = sizeof(MDL) +
                  ADDRESS_AND_SIZE_TO_SPAN_PAGES(Buffer, Length) * sizeof(ULONG);
            MdlAddress->StartVa = (PVOID)PAGE_ALIGN(Buffer);
            MdlAddress->ByteCount = Length;
            MdlAddress->ByteOffset = BYTE_OFFSET(Buffer);
            PageFrame = (PULONG)(MdlAddress + 1);
            FrameNumber = (((ULONG)MdlAddress->StartVa) & 0x1fffffff) >> PAGE_SHIFT;
            NumberOfPages = (MdlAddress->ByteCount +
                  MdlAddress->ByteOffset + PAGE_SIZE - 1) >> PAGE_SHIFT;
            for (Index = 0; Index < NumberOfPages; Index += 1) {
                *PageFrame++ = FrameNumber++;
            }

            //
            // Flush I/O buffers and write to the boot device.
            //

            HalFlushIoBuffers(MdlAddress, FALSE, TRUE);
            ArcStatus = FloppyBootIO(MdlAddress,
                                     BlFileTable[FileId].Position.LowPart >> SECTOR_SHIFT,
                                     FileId,
                                     TRUE);

            if (ArcStatus == ESUCCESS) {
                BlFileTable[FileId].Position.LowPart += Length;
                *Count = Length;
                return ESUCCESS;
            } else {
                *Count = 0;
                return EIO;
            }
        }
    }
}

ARC_STATUS
FloppyGetReadStatus (
    IN ULONG FileId
    )

/*++

Routine Description:


Arguments:


Return Value:


--*/

{
    return ESUCCESS;
}

ARC_STATUS
FloppySeek (
    IN ULONG FileId,
    IN PLARGE_INTEGER Offset,
    IN SEEK_MODE SeekMode
    )

/*++

Routine Description:

    This function sets the device position to the specified offset for
    the specified file id.

Arguments:

    FileId - Supplies the file table index.

    Offset - Supplies to new device position.

    SeekMode - Supplies the mode for the position.

Return Value:

    ESUCCESS is returned.

--*/

{

    //
    // Set the current device position as specifed by the seek mode.
    //

    if (SeekMode == SeekAbsolute) {
        BlFileTable[FileId].Position.LowPart = Offset->LowPart;

    } else if (SeekMode == SeekRelative) {
        BlFileTable[FileId].Position.LowPart += Offset->LowPart;
    }

    return ESUCCESS;
}

ARC_STATUS
FloppyGetFileInformation (
    IN ULONG FileId,
    OUT PFILE_INFORMATION Finfo
    )

/*++

Routine Description:

    This routine returns the floppy size.

Arguments:

    FileId - Supplies the file table index.

    Finfo - Supplies a pointer to where the File Information is stored.

Return Value:

    ESUCCESS is returned.

--*/

{

    RtlZeroMemory(Finfo, sizeof(FILE_INFORMATION));

    switch (BlFileTable[FileId].u.FloppyContext.DriveType) {

        case DRIVE_TYPE_1200: Finfo->EndingAddress.LowPart = 1200 * 1024;
                              break;
        case DRIVE_TYPE_1440: Finfo->EndingAddress.LowPart = 1440 * 1024;
                              break;
        case DRIVE_TYPE_2880: Finfo->EndingAddress.LowPart = 2880 * 1024;
                              break;

        default             : return EINVAL;
    }

    Finfo->CurrentPosition = BlFileTable[FileId].Position;
    Finfo->Type = FloppyDiskPeripheral;
    return ESUCCESS;
}


VOID
FloppyBootSetup(
    VOID
    )

/*++

Routine Description:

    This routine is invoked to initialize the floppy boot device before any
    other floppy operations are attempted.  This routine performs the following
    operations to initialize the device:

        o  Clear the reset and DMA gate flags in the DOR
        o  Reset the floppy by writing the s/w reset in the DSR
        o  Set the program data rate in the CCR
        o  Issue a sense interrupt command and read the four statuses back
        o  Issue a configure command
        o  Issue a specify command

Arguments:

    None.

Return Value:

    None.

--*/

{

    ULONG i,j;
    //
    // Begin by clearing the reset and DMA gate flags in the DOR.
    //

    WRITE_PORT_UCHAR((PUCHAR)&FLOPPY_CONTROL->DigitalOutput, 0x0c);
    FwStallExecution(MICROSECONDS_500);

    //
    // Reset the floppy controller by setting the s/w reset bit in the DSR.
    //

    WRITE_PORT_UCHAR((PUCHAR)&FLOPPY_CONTROL->MsrDsr.DataRateSelect, 0x80);
    FwStallExecution(MILLISECONDS_30);

    //
    // Set the data rate in the CCR.
    //

//    WRITE_PORT_UCHAR(&FLOPPY_CONTROL->DirCcr.ConfigurationControl, 0);

//    if (FwWaitForDeviceInterrupt(1 << (FLOPPY_VECTOR - DEVICE_VECTORS - 1),
//                                 FW_FLOPPY_TIMEOUT)) {
    if (WaitForFloppyInterrupt(FW_FLOPPY_TIMEOUT)) {
//        FwPrint("Floppy setup timeout\r\n");
        return;
    }

    FwStallExecution(MICROSECONDS_20);


    //
    // Issue the sense interrupt command and read back the status and
    // relative cylinder number from the controller.  This is done for
    // each of the four possible devices.  Note that the output is always
    // ignored.
    //

    for (i = j = 0; i <= 3; i++) {
        SendByte(COMMND_SENSE_INTERRUPT);
        DebugByte[j++] = ReceiveByte();
        DebugByte[j++] = ReceiveByte();
    }

    //
    // Issue the configuration command.
    //

    SendByte( COMMND_CONFIGURE );  // command
    SendByte( 0x00 );           // required 0
    SendByte( 0x58 );           // implied seeks, disable polling & threshold = 8
    SendByte( 0x00 );           // precompensation track = 0

    //
    // Issue the specify command.
    //

    SendByte( COMMND_SPECIFY );    // command
    SendByte( 0xdf );           // step rate time=d, head unload=f
    SendByte( 0x03 );           // head load=1, DMA disabled

    return;
}

VOID
FloppyInitialize(
    IN OUT PDRIVER_LOOKUP_ENTRY LookupTable,
    IN ULONG Entries
    )

/*++

Routine Description:

    This routine initializes the FloppyDriver.

Arguments:

    LookupTable - Pointer to the driver lookup table where the pathnames
                  recognized by this driver will be stored.

    Entries     - Number of entries in the table.

Return Value:

    None.

--*/

{
    PCONFIGURATION_COMPONENT FloppyComponent;
    CHAR FloppyPath[32];
    ULONG Drive;
    //
    // Set motor status to all motors stopped.
    //

    MotorStatus = MAXLONG;

    FloppyBootSetup();

    //
    // Default to floppy 0 in case no configuration is present in the NVRAM
    //
    LookupTable->DevicePath = FW_FLOPPY_0_DEVICE;
    LookupTable->DispatchTable = &FloppyEntryTable;

    //
    // Get the floppy configuration information.
    //
    FloppyComponent = FwGetComponent(FW_FLOPPY_0_DEVICE);
    if ((FloppyComponent != NULL) &&
        (FloppyComponent->Type == FloppyDiskPeripheral)) {

        //
        // Initialize the lookup table.
        //

        LookupTable->DevicePath = FW_FLOPPY_0_DEVICE;
        LookupTable->DispatchTable = &FloppyEntryTable;
        LookupTable++;

        //
        // Return if no more room in the lookup table.
        //

        if (Entries == 1) {
            return;
        }
    }
    FloppyComponent = FwGetComponent(FW_FLOPPY_1_DEVICE);
    if ((FloppyComponent != NULL) &&
        (FloppyComponent->Type == FloppyDiskPeripheral)) {

        //
        // Initialize the lookup table.
        //
        LookupTable->DevicePath = FW_FLOPPY_1_DEVICE;
        LookupTable->DispatchTable = &FloppyEntryTable;
    }
}

ARC_STATUS
FloppyBootClose(
    )

/*++

Routine Description:

    This routine shuts down the floppy after the boot has taken place.

Arguments:

    None.

Return Value:

    Normal, successful completion status.

--*/

{

    //
    // Turn the floppy drive's motor off and indicate that the
    // motor has been shut off.
    //

    WRITE_PORT_UCHAR((PUCHAR)&FLOPPY_CONTROL->DigitalOutput, 0xc);
    MotorStatus = MAXLONG;


    return ESUCCESS;
}

ARC_STATUS
FloppyBootIO (
    IN PMDL Mdl,
    IN ULONG StartingBlock,
    IN ULONG FileId,
    IN BOOLEAN ReadWrite
    )

/*++

Routine Description:

    This routine reads or writes blocks from the floppy into the buffer described by
    the MDL.  The size of the read is the number of bytes mapped by the MDL
    and the blocks read start at StartingBlock.

Arguments:

    Mdl - Memory Descriptor List for buffer.

    StartingBlock - Block to begin the read operation.

    FileId - The file identifier of the floppy drive to access.

    ReadWrite - Specifies the kind of transfer to be done
              TRUE = WRITE
              FALSE = READ

Return Value:

    The function value is the status of the operation.


--*/

{

    UCHAR Cylinder;
    UCHAR Head = 0;
    UCHAR Sector;
    UCHAR EndSector;
    ULONG BlockCount;
    ULONG TransferSize;
    ULONG TransferedBytes;
    ULONG i,j,k;
    PUCHAR Buffer;
    BOOLEAN Success;
    ARC_STATUS Status = ESUCCESS;
    UCHAR DriveNumber;
    ULONG SectorsPerTrack;

    DriveNumber = BlFileTable[FileId].u.FloppyContext.DiskId;
    SectorsPerTrack = BlFileTable[FileId].u.FloppyContext.SectorsPerTrack;

    //
    // Enable the drive and start the motor via the DOR.
    //

    if (MotorStatus != DriveNumber) {
        WRITE_PORT_UCHAR((PUCHAR)&FLOPPY_CONTROL->DigitalOutput,
                             ((0xc + DriveNumber) + (1 << (DriveNumber + 4))));
        MotorStatus = DriveNumber;

        //
        // Wait for at least 500ms to ensure that the motor really is running.
        //

        FwStallExecution(MILLISECONDS_500);
    }

    //
    // Get the number of blocks that need to be read in order to fulfill
    // this request.  Also set the base address of the caller's buffer.
    //

    Buffer = ((PUCHAR) Mdl->StartVa) + Mdl->ByteOffset;

    //
    // Get the parameters for the read command.
    //

    Cylinder = StartingBlock / (SectorsPerTrack * 2);
    Sector = (StartingBlock % SectorsPerTrack) + 1;
    Head = (StartingBlock / SectorsPerTrack) % 2;

    ClearFloppyFifo();

    //
    // Loop reading blocks from the device until the request has been
    // satisfied.
    //

    for (BlockCount = Mdl->ByteCount >> 9; BlockCount > 0; ) {

        //
        // Determine the size of this read based on the number of blocks
        // required and where the current sector is on the current track.
        //

        EndSector = MINIMUM( SectorsPerTrack, (Sector + (BlockCount - 1)) );
        TransferSize = (EndSector - Sector) + 1;
        BlockCount -= TransferSize;
        TransferSize <<= 9;

        //
        // Attempt to read the block(s) up to RETRY_COUNT times.
        //

        for (k = 0; k < RETRY_COUNT; k++) {

            //
            // Assume that the operation will be successful.
            //

            Success = TRUE;

            //
            // Do an explicit seek if this is a 360 K disk in a 1.2 MB drive.
            //

            if (CurrentDriveMediaConstants->CylinderShift != 0) {
                if (!SendByte( COMMND_SEEK ) ||
                    !SendByte( (Head << 2) + DriveNumber ) ||  // head select & drive
                    !SendByte( Cylinder << CurrentDriveMediaConstants->CylinderShift )) {
                    return(EIO);
                }

//                if (FwWaitForDeviceInterrupt(1 << (FLOPPY_VECTOR - DEVICE_VECTORS - 1),
//                                             FW_FLOPPY_TIMEOUT)) {
                if (WaitForFloppyInterrupt(FW_FLOPPY_TIMEOUT)) {
                    return(EIO);
                }

                //
                // Send the sense interrupt command.
                //

                if (!SendByte( COMMND_SENSE_INTERRUPT )) {    // command
                    return(EIO);
                }

                //
                // Read back the information from the drive and check the status of the
                // recalibrate command.
                //

                DebugByte[0] = ReceiveByte();
                DebugByte[1] = ReceiveByte();

                if (DebugByte[1] != (Cylinder << CurrentDriveMediaConstants->CylinderShift)) {
                    return(EIO);
                }

                //
                // Now try to read the ID from wherever we're at.
                //

                if (!SendByte( COMMND_READ_ID + COMMND_MFM ) ||  // command
                    !SendByte( DriveNumber | ((CurrentDriveMediaConstants->NumberOfHeads - 1) << 2) )) {
                    return(EIO);
                }

//                if (FwWaitForDeviceInterrupt(1 << (FLOPPY_VECTOR - DEVICE_VECTORS - 1),
//                                             FW_FLOPPY_TIMEOUT)) {
                if (WaitForFloppyInterrupt(FW_FLOPPY_TIMEOUT)) {
                    return(EIO);
                }

                for (i = 0; i < 7; i++) {
                    DebugByte[i] = ReceiveByte();
                }

                if ( ( DebugByte[0] !=
                        (UCHAR)(DriveNumber |
                                ((CurrentDriveMediaConstants->NumberOfHeads - 1) << 2))) ||
                    ( DebugByte[1] != 0 ) ||
                    ( DebugByte[2] != 0 ) ) {

                    return(EIO);
                }

            }

            //
            // Send the command and parameters for the operation.
            //

            if (ReadWrite == TRUE) {
                if (!SendByte( COMMND_WRITE_DATA + COMMND_MFM )) {
                    return(EIO);
                }
            } else {
                if (!SendByte( COMMND_READ_DATA + COMMND_MFM )) {
                    return(EIO);
                }
            }
            if (!SendByte( (Head << 2) + DriveNumber ) ||  // head select & drive
                !SendByte( Cylinder ) ||                   // cylinder
                !SendByte( Head ) ||                       // head
                !SendByte( Sector ) ||                     // sector
                !SendByte( 2 ) ||                          // sector size; 2 => 512B/sec
                !SendByte( EndSector ) ||                  // end of track sector
                !SendByte( CurrentDriveMediaConstants->ReadWriteGapLength ) ||
                !SendByte( 0xff )) {                       // special sector size
                return(EIO);
            }

            //
            // Ensure that the floppy drive does not time-out.
            //

            for (j = 0; j < SECONDS_2; j += MICROSECONDS_10) {
                if (READ_PORT_UCHAR((PUCHAR)&FLOPPY_CONTROL->MsrDsr.MainStatus)
                    > 127) {
                    break;
                }

                FwStallExecution(MICROSECONDS_10);
            }

            //
            // Check for time-out;  if one occurred, then return unsuccessful
            // status.
            //

            if (j == SECONDS_2) {
//                FwPrint("Floppy timeout\r\n");
                return EIO;
            }

            //
            // Read the data from the appropriate block(s) and check the number
            // of bytes actually read.
            //

            if (ReadWrite == TRUE) {
                WriteFloppyFifo(Buffer,TransferSize);
            } else {
                TransferedBytes = ReadFloppyFifo(Buffer);
                if (TransferedBytes != TransferSize) {
                    Success = FALSE;
                }
            }

            //
            // Read the status information from the device.
            //

            while (READ_PORT_UCHAR((PUCHAR)&FLOPPY_CONTROL->MsrDsr.MainStatus)
                   <= 127) {
            }

            for (i = 0; i < 7; i++) {
                DebugByte[i] = ReceiveByte();
            }

            if (((DebugByte[0] >> 4) == 2) &&
                (DebugByte[1] == 0) &&
                (DebugByte[2] == 0) &&
                Success) {
                ;

            } else {

                if ((DebugByte[0] >> 4) != 4) {
                    Success = FALSE;
                }

                if (DebugByte[1] != 0x80) {
                    Success = FALSE;
                }

                if (DebugByte[2] != 0) {
                    Success = FALSE;
                }
            }

            //
            // If the operation was successful, exit the loop.
            //

            if (Success) {
		Buffer += TransferSize;
                break;
            }

            //
            // The operation did not work.  Attempt to recalibrate the
            // device and wait for everything to settle out, and then
            // try the operation again.
            //

            if (!Recalibrate(DriveNumber)) {
//                FwPrint("Floppy recalibration error\r\n");
                return(EIO);
            }
            FwStallExecution(MILLISECONDS_15);
        }

        //
        // If the operation was not successful after RETRY_COUNT tries, get
        // out now.
        //

        if (!Success) {
            Status = EIO;
            break;
        }

        //
        // If more data is needed, get the next place to read from.  Note
        // that if there is more data to be read, then the last sector
        // just read was the last sector on this head.
        //

        if (BlockCount > 0) {
            if (Head == 1) {
                Cylinder += 1;
                Head = 0;
            } else {
                Head = 1;
            }
            Sector = 1;
        }

        if (Success) {
            Status = ESUCCESS;
        }
    }
    return Status;
}

BOOLEAN
Recalibrate(
    UCHAR DriveNumber
    )

/*++

Routine Description:

    This routine issues a recalibrate command to the device, waits for it to
    interrupt, sends it a sense interrupt command, and checks the result to
    ensure that the recalibrate command worked properly.

Arguments:

    DriveNumber - Supplies the Floppy drive to recalibrate.

Return Value:

    Returns TRUE if the recalibrate was successful, FALSE if not.

--*/

{

    //
    // Send the recalibrate command to the device.
    //

    if (!SendByte( COMMND_RECALIBRATE ) ||    // command
        !SendByte( DriveNumber )) {           // drive select
        return(FALSE);
    }

//    if (FwWaitForDeviceInterrupt(1 << (FLOPPY_VECTOR - DEVICE_VECTORS - 1),
//                                 FW_FLOPPY_TIMEOUT)) {
    if (WaitForFloppyInterrupt(FW_FLOPPY_TIMEOUT)) {
//        FwPrint("Floppy recalibrate timeout\r\n");
        return(FALSE);
    }

    //
    // Send the sense interrupt command.
    //

    if (!SendByte( COMMND_SENSE_INTERRUPT )) {    // command
        return(FALSE);
    }

    //
    // Read back the information from the drive and check the status of the
    // recalibrate command.
    //

    DebugByte[0] = ReceiveByte();
    if ((DebugByte[0] >> 4) != 2) {
        return FALSE;
    }

    DebugByte[1] = ReceiveByte();

    if ((READ_PORT_UCHAR((PUCHAR)&FLOPPY_CONTROL->MsrDsr.MainStatus) &
         STATUS_IO_READY_MASK) == STATUS_READ_READY) {
        return FALSE;
    }

    return TRUE;
}

UCHAR
ReceiveByte(
    )

/*++

Routine Description:

    This routine reads the next byte from the floppy FIFO register.

Arguments:

    None.

Return Value:

    The function value is the value of the byte read from the floppy FIFO.

--*/

{

    ULONG i;

    //
    // Check status register for readiness to receive data.
    //

    for (i = 0; i < MICROSECONDS_250; i += MICROSECONDS_10) {
        if ((READ_PORT_UCHAR((PUCHAR)&FLOPPY_CONTROL->MsrDsr.MainStatus) &
             STATUS_IO_READY_MASK) == STATUS_READ_READY) {
            return READ_PORT_UCHAR((PUCHAR)&FLOPPY_CONTROL->Fifo);
        }

        FwStallExecution(MICROSECONDS_10);
    }

    //
    // A timeout occurred while attempting to read data from the floppy fifo.
    // Output an error message and return.
    //

//    FwPrint("Error reading from floppy fifo\r\n");
    return(0xFF);
}

BOOLEAN
SendByte(
    IN UCHAR SourceByte
    )

/*++

Routine Description:

    This routine sends a specified byte to the floppy FIFO register.

Arguments:

    SourceByte - Byte to be sent to the controller.

Return Value:

    If the byte was successfully written to the floppy FIFO, TRUE is returned,
    otherwise FALSE is returned.

--*/

{

    ULONG i;

    //
    // Check status register for readiness to receive data.
    //

    for (i = 0; i < MICROSECONDS_250; i += MICROSECONDS_10) {
        if ((READ_PORT_UCHAR((PUCHAR)&FLOPPY_CONTROL->MsrDsr.MainStatus) &
             STATUS_IO_READY_MASK) == STATUS_WRITE_READY) {
            WRITE_PORT_UCHAR((PUCHAR)&FLOPPY_CONTROL->Fifo, SourceByte);
            return(TRUE);
        }

        FwStallExecution(MICROSECONDS_10);
    }

    //
    // A timeout occurred while attempting to write data to the floppy fifo.
    // Output an error message and return.
    //

//    FwPrint("Error writing to floppy fifo\r\n");
    return(FALSE);
}

VOID
ClearFloppyFifo(
    IN VOID
    )

/*++

Routine Description:

    This routine empties the fifo.

Arguments:

    None.

Return Value:

    None.

--*/

{

    ULONG i;

    //
    // Check status register for readiness to receive data.
    //
    while ((READ_PORT_UCHAR((PUCHAR)&FLOPPY_CONTROL->MsrDsr.MainStatus) &
             STATUS_IO_READY_MASK) == STATUS_READ_READY) {
        READ_PORT_UCHAR((PUCHAR)&FLOPPY_CONTROL->Fifo);
        FwStallExecution(MICROSECONDS_10);
    }
}


ARC_STATUS
FloppyDatarateSpecifyConfigure(
    IN DRIVE_MEDIA_TYPE DriveMediaType,
    IN UCHAR DriveNumber
    )

/*++

Routine Description:

    This routine is called to set up the controller every time a new type
    of diskette is to be accessed.  It issues the CONFIGURE command,
    does a SPECIFY, sets the data rate, and RECALIBRATEs the drive.

Arguments:

    DriveMediaType - supplies the drive type/media density combination.

    DriveNumber - supplies the drive number.

Return Value:

    ESUCCESS if the controller is properly prepared; appropriate
    error propogated otherwise.

--*/

{
    UCHAR Configure;

    //
    // Don't enable implied seeks when there is a 360K disk in a 1.2M drive.
    //

    if (DriveMediaConstants[DriveMediaType].CylinderShift) {
        Configure = 0x18;
    } else {
        Configure = 0x58;
    }

    //
    // Issue the configuration command.
    //

    if (!SendByte( COMMND_CONFIGURE ) ||  // command
        !SendByte( 0x00 ) ||              // required 0
        !SendByte( Configure ) ||         // implied seeks, disable polling & threshold = 8
        !SendByte( 0x00 ) ||              // precompensation track = 0

    //
    // Issue SPECIFY command to program the head load and unload
    // rates, the drive step rate, and the DMA data transfer mode.
    //

        !SendByte( COMMND_SPECIFY ) ||    // command
        !SendByte( DriveMediaConstants[DriveMediaType].StepRateHeadUnloadTime) ||
        !SendByte( DriveMediaConstants[DriveMediaType].HeadLoadTime + 1)) {
        return(EIO);
    }

    //
    // Program the data rate
    //

    WRITE_PORT_UCHAR((PUCHAR)&FLOPPY_CONTROL->MsrDsr.DataRateSelect,
                         DriveMediaConstants[DriveMediaType].DataTransferRate );

    //
    // Recalibrate the drive, now that we've changed all its
    // parameters.
    //

    if (Recalibrate(DriveNumber)) {
        return(ESUCCESS);
    } else {
//        FwPrint("Floppy recalibration error\r\n");
        return(EIO);
    }
}

ARC_STATUS
FloppyDetermineMediaType(
    IN OUT PFLOPPY_CONTEXT FloppyContext,
    OUT PMEDIA_TYPE mediaType
    )

/*++

Routine Description:

    This routine is called by FloppyBootIO() when the media type is
    unknown.  It assumes the largest media supported by the drive is
    available, and keeps trying lower values until it finds one that
    works.

Arguments:

    FloppyContext - supplies a pointer to the floppy context structure.

    mediaType - supplies a pointer to a variable that receives the 
                type of the media in the drive.

Return Value:

    ESUCCESS if the type of the media is determined; appropriate
    error propogated otherwise.

--*/

{
    ARC_STATUS Status;
    BOOLEAN mediaTypesExhausted = FALSE;
    DRIVE_MEDIA_TYPE DriveMediaType;
    UCHAR DriveNumber;
    ULONG i;

    //
    // Assume that the largest supported media is in the drive.  If that
    // turns out to be untrue, we'll try successively smaller media types
    // until we find what's really in there (or we run out and decide
    // that the media isn't formatted).
    //

    DriveMediaType =
        DriveMediaLimits[FloppyContext->DriveType].HighestDriveMediaType;

    DriveNumber = FloppyContext->DiskId;

    do {

        Status = FloppyDatarateSpecifyConfigure( DriveMediaType, DriveNumber );

        if ( Status != ESUCCESS ) {

            //
            // The SPECIFY or CONFIGURE commands resulted in an error.
            // Force ourselves out of this loop and return error.
            //

            mediaTypesExhausted = TRUE;

        } else {

            CurrentDriveMediaConstants = &DriveMediaConstants[DriveMediaType];

            //
            // Now try to read the ID from wherever we're at.
            //

            if (!SendByte( COMMND_READ_ID + COMMND_MFM ) ||  // command
                !SendByte( DriveNumber | ((CurrentDriveMediaConstants->NumberOfHeads - 1) << 2) )) {
                return(EIO);
            }

//            if (FwWaitForDeviceInterrupt(1 << (FLOPPY_VECTOR - DEVICE_VECTORS - 1),
//                                         FW_FLOPPY_TIMEOUT)) {
	    if (WaitForFloppyInterrupt(FW_FLOPPY_TIMEOUT)) {
//                FwPrint("Floppy determine media timeout\r\n");
                return(EIO);
            }

            for (i = 0; i < 7; i++) {
                DebugByte[i] = ReceiveByte();
            }

            if ( ( DebugByte[0] !=
                    (UCHAR)(DriveNumber |
                            ((CurrentDriveMediaConstants->NumberOfHeads - 1) << 2))) ||
                ( DebugByte[1] != 0 ) ||
                ( DebugByte[2] != 0 ) ) {

                DriveMediaType--;

                Status = ENXIO;

                //
                // Next comparison must be signed, for when
                // LowestDriveMediaType = 0.
                //

                if ( (CHAR)( DriveMediaType ) <
                     (CHAR)( DriveMediaLimits[FloppyContext->DriveType].LowestDriveMediaType )) {

                    mediaTypesExhausted = TRUE;

                }
            }
        }

    } while ( ( ( Status != ESUCCESS ) ) && !( mediaTypesExhausted ) );

    if ( Status == ESUCCESS ) {

        FloppyContext->SectorsPerTrack =
            CurrentDriveMediaConstants->SectorsPerTrack;

	*mediaType = CurrentDriveMediaConstants->MediaType;
    }
    return Status;
}

ULONG
ReadFloppyFifo (
    IN OUT PUCHAR Buffer
    )

/*++

Routine Description:

    This reads data from the floppy fifo.

Arguments:

    Buffer - Pointer to the buffer that receives the data read.

Return Value:

    The number of bytes read.

--*/
{
    ULONG	BytesRead = 0;
    UCHAR	MSR;

    while (TRUE) {

	// Loop until DIO and RQM are both set.
	while (
	       ((MSR = READ_PORT_UCHAR((PUCHAR)&FLOPPY_CONTROL->MsrDsr.MainStatus))
		& (FLOPPY_MSR_DIO | FLOPPY_MSR_RQM))
	       != (FLOPPY_MSR_DIO | FLOPPY_MSR_RQM)
	       ) {
	}

	// If the non-DMA bit is clear, end of transfer.
	if ((MSR & FLOPPY_MSR_NONDMA) == 0) {
	    return (BytesRead);
	}

	// Read the byte, increment pointer and total, until done.
	*Buffer++ = READ_PORT_UCHAR((PUCHAR)&FLOPPY_CONTROL->Fifo);
	BytesRead++;
    }
}

VOID
WriteFloppyFifo(
    IN PUCHAR Buffer,
    IN ULONG Size
    )

/*++

Routine Description:

    This writes data to the floppy fifo.

Arguments:

    Buffer - Pointer to the buffer that contains the data to be written.

    Size - The number of bytes to be written.

Return Value:

    None.

--*/

{
    while (Size) {

	// Loop until DIO is clear and RQM is set.
	while (
	       (READ_PORT_UCHAR((PUCHAR)&FLOPPY_CONTROL->MsrDsr.MainStatus)
		& (FLOPPY_MSR_DIO | FLOPPY_MSR_RQM))
	       != FLOPPY_MSR_RQM
	       ) {
	}

	//
	// Write the byte, increment pointer, decrement count, until done.
	// Remember, C does call by value.
	//

	WRITE_PORT_UCHAR((PUCHAR)&FLOPPY_CONTROL->Fifo, *Buffer++);
	--Size;
    }

    //
    // All bytes written to fifo.  Wait until the floppy controller
    // empties it.
    //

    while (
	   (READ_PORT_UCHAR((PUCHAR)&FLOPPY_CONTROL->MsrDsr.MainStatus)
	    & FLOPPY_MSR_NONDMA)
	   != 0 ) {
    }


    return;
	
}

ARC_STATUS
WaitForFloppyInterrupt(
	ULONG Timeout
	)

/*++
	
Routine Description:

    This waits until the floppy controller has an interrupt pending,
    or the requested Timeout period has been reached.

    The Jazz code enables interrupts for the keyboard and individual
    devices.  In actuality, the only non-keyboard device it enables interrupts
    for is the floppy controller.  For Alpha we avoid dealing
    with interrupts in the firmware code to keep the PALcode simple.  So
    every Jazz call to "FwWaitForDeviceInterrupt" has been changed to
    this function.

Arguments:

    Timeout - A timeout value in seconds.  Note that a timeout of 0 gives
              an actual timeout of between 0 and 1, a timeout of 1 gives
              an actual timeout of between 1 and 2, and so on.

Return Value:

    ESUCCESS if an interrupt is floppy interrupt is detected.
    EIO      if a timeout occurs.

--*/

{
    ULONG Time1;


    Time1 = FwGetRelativeTime();

    // Ask for the interrupt wires
    WRITE_PORT_UCHAR((PUCHAR)EISA_INT_OCW3, EISA_INT_OCW3_IRR);

    //
    // 0x40 is irq6, which is the floppy controller.  I should make this
    // into a #define.
    //
    while (
	   (READ_PORT_UCHAR((PUCHAR)EISA_INT_OCW3) & 0x40)
	   == 0) {
        if ((FwGetRelativeTime() - Time1) > (Timeout + 1)) {
            return(EIO);
        }
    }
    
    return(ESUCCESS);
}