File: disk.c

package info (click to toggle)
faumachine 20100527-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 53,836 kB
  • ctags: 20,552
  • sloc: ansic: 179,550; asm: 3,645; makefile: 3,611; perl: 2,103; sh: 1,529; python: 600; xml: 563; lex: 210; vhdl: 204
file content (2898 lines) | stat: -rw-r--r-- 65,702 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
/*
 * $Id: disk.c,v 1.230 2009-03-30 14:10:25 vrsieh Exp $ 
 *
 * Copyright (C) 2004-2009 FAUmachine Team <info@faumachine.org>.
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */

#include "build_config.h"
#include "compiler.h"

/* ===================== RUNTIME || INIT ======================== */
#if defined(RUNTIME_RM) || defined(INIT_RM)

#define FD_CHANGELINE_SUPPORT	1

#define IDE_ERR_STAT	0x01
#define IDE_INDEX_STAT	0x02
#define IDE_ECC_STAT	0x04
#define IDE_DRQ_STAT	0x08
#define IDE_SEEK_STAT	0x10
#define IDE_WRERR_STAT	0x20
#define IDE_READY_STAT	0x40
#define IDE_BUSY_STAT	0x80

#define WIN_RESTORE	0x10 /* Recalibrate Device */
#define WIN_IDENTIFY    0xec /* Identify ATA Device */
#define WIN_PIDENTIFY   0xa1 /* Identify ATAPI Device */

struct diskette_param_table_t {
	/* FIXME VOSSI */
	uint8_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10;
};

extern const uint16_t ide_port_table[];
extern const struct diskette_param_table_t diskette_param_table;

#endif /* RUNTIME_RM || INIT_RM */
/* ==================== RUNTIME_RM ==================== */
#ifdef RUNTIME_RM

CODE16;

#include "assert.h"
#include "fixme.h"
#include "stdio.h"
#include "string.h"
#include "in.h"

#include "io.h"
#include "const.h"
#include "cmos.h"
#include "var.h"
#include "el_torito.h"
#include "ptrace.h"
#include "disk.h"

#define ATAPI_TEST_UNIT_READY	0x00
#define ATAPI_REQUEST_SENSE	0x03
#define ATAPI_READ_SECTOR	0x28

#define ATAPI_NO_SENSE		0
#define ATAPI_NOT_READY		2
#define ATAPI_UNIT_ATTENTION	6

struct atapi_compacket {
	unsigned char opcode;
	unsigned char reserved0;
	uint32_t lba;
	unsigned char reserved1;
	union {
		unsigned short transfer_length;
		unsigned short paramlist_length;
		unsigned short allocation_length;
	} misc;
	unsigned char reserved2;
	unsigned short reserved3;
} __attribute__((__packed__));

struct atapi_request_sense {
	/* Byte 0: */
	/*	Bit 0-6: error code */
	/*	Bit 7: valid */
	unsigned char error_code;
	/* Byte 1: */
	unsigned char segment_number;
	/* Byte 2: */
	/*	Bit 0-3: sense_key */
	/*	Bit 4: reserved */
	/*	Bit 5: ili */
	/*	Bit 6-7: reserved */
	unsigned char sense_key;
	/* Byte 3-6: */
	unsigned char information[4];
	/* Byte 7: */
	unsigned char add_sense_len;
	/* Byte 8-11: */
	unsigned char command_info[4];
	/* Byte 12: */
	unsigned char asc;
	/* Byte 13: */
	unsigned char ascq;
	/* Byte 14: */
	unsigned char fruc;
	/* Byte 15-17: */
	unsigned char sks[3];
};

struct Int13DPT {
	unsigned short size;
	unsigned short flags;
	uint32_t cyls;
	uint32_t heads;
	uint32_t secs;
	uint32_t total_low;	/* Should be long long */
	uint32_t total_high;
	unsigned short bytes_per_sec;
	/* ... */
};


struct Int13Ext {
	unsigned char size;
	unsigned char reserved;
	unsigned short count;
	unsigned short offset;
	unsigned short segment;
#if 0	/* FIXME VOSSI */
	unsigned long long lba;
#else
	uint32_t lba;
	uint32_t lba2;
#endif
};

/*
 * Since no provisions are made for multiple drive types, most
 * values in this table are ignored.  I set parameters for 1.44M
 * floppy here
 */
/* Should be located at 0xefc7 in ROM. FIXME VOSSI */

const struct diskette_param_table_t diskette_param_table = {
	0xAF,
	0x02, /* Head load time 0000001, DMA used. */
	0x25,
	0x02,
	18,
	0x1B,
	0xFF,
	0x6C,
	0xF6,
	0x0F,
	0x08
};

CONST unsigned short ide_port_table[] = {
	/* hda/hdb */ 0x01f0,
	/* hdc/hdd */ 0x0170,
	/* hde/hdf */ 0x01e8,
	/* hdg/hdh */ 0x0168,
	/* hdi/hdj */ 0x01e0,
	/* hdk/hdl */ 0x0160,
};

static void
int13_fail(struct regs *regs, unsigned char status)
{
	if (DL < 2) {
		var_put(fd_res, status);
	} else {
		var_put(hd_res, status);
	}

	AH = status;
	F |= 1 << 0;	/* Set carry. */
}

static void
int13_success(struct regs *regs, unsigned char status)
{
	if (DL < 2) {
		var_put(fd_res, status);
	} else {
		var_put(hd_res, status);
	}

	AH = status;
	F &= ~(1 << 0);	/* Clear carry. */
}

unsigned char
ide_harddisk_read_chs(
	unsigned char device,
	unsigned char num, 
	unsigned short cylinder, 
	unsigned char head,
	unsigned char sector,
	unsigned short buffer_seg,
	unsigned short buffer_off)
{
	uint16_t port;
	uint8_t status;

	assert(/* 0 <= device / 2 && */ device / 2 < 6);
	assert(/* 0 <= device % 2 && */ device % 2 < 2);

	/* Get port addresses. */
	port = const_get(ide_port_table[device / 2]);

	/* Wait while busy. */
	do {
		status = inb(port + 0x206);
	} while (status & IDE_BUSY_STAT);

	/*
	 * Send read command.
	 */
	outb(num, port + 2);		/* number of sectors */
	outb(sector & 0x3f, port + 3);	/* sector */
	outb(cylinder & 0xff, port + 4);/* cylinder (low bits) */
	outb(cylinder >> 8, port + 5);	/* cylinder (high bits) */
	outb(0xa0 | ((device % 2) << 4) | head, port + 6);
					/* unit / head */
	outb(0x20, port + 7);		/* command: read */

	/*
	 * Get all sectors.
	 */
	for ( ; 0 < num; num--) {
		unsigned int count;
		unsigned short data;

		/* Wait while busy. */
		do {
			status = inb(port + 0x206);
		} while (status & IDE_BUSY_STAT);

		if (status & IDE_ERR_STAT) {
			/* ERR bit set */
			assert(0);
			return 0xff;	/* FIXME VOSSI */
		}

		if (! (status & IDE_DRQ_STAT)) {
			/* FIXME VOSSI */
			assert(0);
			return 0xff;
		}

		buffer_seg += buffer_off / 16;
		buffer_off %= 16;

		for (count = 0; count < 512 / 2; count++) {
			data = inw(port);
			put_word(buffer_seg, buffer_off + count * 2, data);
		}

		buffer_seg += 512 / 16;
	}

	return 0;
}

static unsigned char
ide_harddisk_write_chs(
	unsigned char device,
	unsigned char num, 
	unsigned short cylinder, 
	unsigned char head,
	unsigned char sector,
	unsigned short buffer_seg,
	unsigned short buffer_off
)
{
	uint16_t port;
	uint8_t status;

	assert(/* 0 <= device / 2 && */ device / 2 < 6);
	assert(/* 0 <= device % 2 && */ device % 2 < 2);

	/* Get port addresses. */
	port = const_get(ide_port_table[device / 2]);

	/* Wait while busy. */
	do {
		status = inb(port + 0x206);
	} while (status & IDE_BUSY_STAT);

	/*
	 * Send write command.
	 */
	outb(num, port + 2);		/* number of sectors */
	outb(sector & 0x3f, port + 3);	/* sector */
	outb(cylinder & 0xff, port + 4);/* cylinder (low bits) */
	outb(cylinder >> 8, port + 5);	/* cylinder (high bits) */
	outb(0xa0 | ((device % 2) << 4) | head, port + 6);
					/* unit / head */
	outb(0x30, port + 7);		/* command: write */

	/* Wait while busy. */
	do {
		status = inb(port + 0x206);
	} while (status & IDE_BUSY_STAT);

	/*
	 * Send all sectors.
	 */
	for ( ; 0 < num; num--) {
		unsigned int count;
		unsigned short data;

		if (! (status & IDE_DRQ_STAT)) {
			/* FIXME VOSSI */
			assert(0);
			return 0xff;
		}

		/* Send sector. */
		buffer_seg += buffer_off / 16;
		buffer_off %= 16;

		for (count = 0; count < 512 / 2; count++) {
			data = get_word(buffer_seg, buffer_off + count * 2);
			outw(data, port);
		}

		buffer_seg += 512 / 16;

		/* Wait while busy. */
		do {
			status = inb(port + 0x206);
		} while (status & IDE_BUSY_STAT);

		if (status & IDE_ERR_STAT) {
			/* ERR bit set */
			assert(0);
			return 0xff;	/* FIXME VOSSI */
		}
	}

	return 0;
}

static unsigned char
ide_harddisk_read_lba(
	unsigned char controller,
	unsigned char unit,
	unsigned char num, 
	uint32_t /* long long */ start,
	unsigned short buffer_seg,
	unsigned short buffer_off
) {
	uint16_t port;
	uint8_t status;

	assert(/* 0 <= controller && */ controller < 6);
	assert(/* 0 <= unit && */ unit < 2);

	/* Get port addresses. */
	port = const_get(ide_port_table[controller]);

	/* Wait while busy. */
	do {
		status = inb(port + 0x206);
	} while (status & IDE_BUSY_STAT);

	/*
	 * Send read command.
	 */
	outb(num, port + 2);			/* number of sectors */
	outb((start >> 0) & 0xff, port + 3);	/* sector */
	outb((start >> 8) & 0xff, port + 4);	/* cylinder (low bits) */
	outb((start >> 16) & 0xff, port + 5);	/* cylinder (high bits) */
	outb(0xe0 | (unit << 4) | ((start >> 24) & 0xf), port + 6);
						/* unit / head */
	outb(0x20, port + 7);			/* command: read */

	/*
	 * Get all sectors.
	 */
	for ( ; 0 < num; num--) {
		unsigned int count;
		unsigned short data;

		/* Wait while busy. */
		do {
			status = inb(port + 0x206);
		} while (status & IDE_BUSY_STAT);

		if (status & IDE_ERR_STAT) {
			/* ERR bit set */
			return 0xff;    /* FIXME VOSSI */
		}

		/* Read result. */
		if (! (status & IDE_DRQ_STAT)) {
			/* FIXME VOSSI */
			assert(0);
			return 0xff;
		}

		buffer_seg += buffer_off / 16;
		buffer_off %= 16;

		for (count = 0; count < 512 / 2; count++) {
			data = inw(port);
			put_word(buffer_seg, buffer_off + count * 2, data);
		}

		buffer_seg += 512 / 16;
	}

	return 0;
}

static unsigned char
ide_harddisk_write_lba(
	unsigned char controller,
	unsigned char unit,
	unsigned char num, 
	uint32_t /* long long */ start,
	unsigned short buffer_seg,
	unsigned short buffer_off)
{
	uint16_t port;
	uint8_t status;

	assert(/* 0 <= controller && */ controller < 6);
	assert(/* 0 <= unit && */ unit < 2);

	/* Get port addresses. */
	port = const_get(ide_port_table[controller]);

	/* Wait while busy. */
	do {
		status = inb(port + 0x206);
	} while (status & IDE_BUSY_STAT);

	/*
	 * Send write command.
	 */
	outb(num, port + 2);			/* number of sectors */
	outb((start >> 0) & 0xff, port + 3);	/* sector */
	outb((start >> 8) & 0xff, port + 4);	/* cylinder (low bits) */
	outb((start >> 16) & 0xff, port + 5);	/* cylinder (high bits) */
	outb(0xe0 | (unit << 4) | ((start >> 24) & 0xf), port + 6);
						/* unit / head */
	outb(0x30, port + 7);			/* command: write */

	/* Wait while busy. */
	do {
		status = inb(port + 0x206);
	} while (status & IDE_BUSY_STAT);

	/*
	 * Send all sectors.
	 */
	for ( ; 0 < num; num--) {
		unsigned int count;
		unsigned short data;

		if (! (status & IDE_DRQ_STAT)) {
			/* FIXME VOSSI */
			assert(0);
			return 0xff;
		}

		/* Write sector. */
		buffer_seg += buffer_off / 16;
		buffer_off %= 16;

		for (count = 0; count < 512 / 2; count++) {
			data = get_word(buffer_seg, buffer_off + count * 2);
			outw(data, port);
		}

		buffer_seg += 512 / 16;

		/* Wait while busy. */
		do {
			status = inb(port + 0x206);
		} while (status & IDE_BUSY_STAT);

		if (status & IDE_ERR_STAT) {
			/* ERR bit set */
			return 0xff;    /* FIXME VOSSI */
		}
	}

	return 0;
}

static void
floppy_poweron(unsigned short drive)
{
	var_put(fmot_tmout, 0x5a); /* About 5sec at 18Hz. */

	if (var_get(fmot_flag) & (1 << drive)) {
		/* Floppy already spinning... */
		return;
	}

	var_put(fmot_flag, var_get(fmot_flag) | (1 << drive));
	outb(0x0c | ((var_get(fmot_flag) & 0x0f) << 4) | drive, 0x3f2);

	/* Wait for floppy spinning up... */
	/* FIXME VOSSI */
}

static ALWAYS_INLINE void
floppy_poweroff(void)
{
	outb(0x0c, 0x3f2);
	var_put(fmot_flag, 0x00);
}

void
floppy_tick(void)
{
	unsigned char timeout;

	timeout = var_get(fmot_tmout);

	if (timeout == 0) {
		/* Nothing to do... */
		return;
	}

	timeout--;
	var_put(fmot_tmout, timeout);

	if (timeout != 0) {
		/* Nothing to do... */
		return;
	}

	/* Power-off motor. */
	floppy_poweroff();
}

static unsigned char
floppy_err(unsigned char st0, unsigned char st1, unsigned char st2)
{
	unsigned char ret;

	if (((st0 >> 6) & 3) == 0) { /* Interrupt code. */
		ret = 0; /* No error. */

	} else {
		/* Error. */
		if ((st1 >> 0) & 1) { /* Missing address mark. */
			ret = 0x02; /* Missing address mark. */
		} else if ((st1 >> 1) & 1) { /* Not writable. */
			ret = 0x03; /* Write protected. */
		} else if ((st1 >> 2) & 1) { /* No data. */
			ret = 0x04; /* Sector not found / read error. */
		} else if ((st1 >> 4) & 1) { /* Overrun/underrun. */
			ret = 0x08; /* DMA overrun/underrun. */
		} else if ((st1 >> 5) & 1) { /* Data error. */
			ret = 0x10; /* CRC checksum error. */
		} else {
			ret = 0x80; /* Unknown error. */ /* FIXME VOSSI */
		}
	}

#if 0
	dprintf("floppy_err: ret=0x%02x\n", ret);
#endif

	return ret;
}

static unsigned char
floppy_read_chs(
	unsigned char drive,
	unsigned char num,
	unsigned char cylinder,
	unsigned char head,
	unsigned char sector,
	unsigned short buffer_seg,
	unsigned short buffer_off
)
{
	unsigned char status;
	unsigned long size;
	unsigned long address;

	if (1 < drive) {
		return 0x01;	/* invalid parameter */
	}

	if (1 < head) {
		return 0x01;	/* invalid parameter */
	}

	size = num * 512 - 1;
	address = ((unsigned long) buffer_seg << 4) + buffer_off;
	if((address & 0xffff) + size > 0xffff) {
		return 0x09;	/* dma boundary */
	}

	/*
	 * Start motor
	 */
	floppy_poweron(drive);
	
	/*
	 * Set up DMA
	 */
	outb(0x06, 0x0a); /* Enable DMA 2. */
	
	outb(0x00, 0x0c); /* Clear byte pointer flip/flop. */
	outb((address >> 0) & 0xff, 0x04); /* Address low byte. */
	outb((address >> 8) & 0xff, 0x04); /* Address high byte. */
	
	outb(0x00, 0x0c); /* Clear byte pointer flip/flop. */
	outb((size >> 0) & 0xff, 0x05); /* Count low byte. */
	outb((size >> 8) & 0xff, 0x05); /* Count high byte. */
	
	/* Mode register: single block, inc addr, no autoinit, read, chan 2 */
	outb((1 << 6) | (0 << 5) | (0 << 4) | (1 << 2) | 2, 0x0b);

	outb((address >> 16) & 0xff, 0x81); /* Page register */

	/*
	 * Wait until DR is ready
	 */
	do {
		status = inb(0x3f4);
	} while ((status & 0xc0) != 0x80);

	/*
	 * Send read command.
	 */
	outb(0xc6, 0x3f5); /* READ with multitrack */
	outb((head << 2) | drive, 0x3f5);
	outb(cylinder, 0x3f5); /* cylinder */
	outb(head, 0x3f5); /* head */
	outb(sector, 0x3f5); /* sector */
	outb(0x02, 0x3f5); /* sectorsize = 0x02 >> 7 */
	outb(18, 0x3f5); /* sector per track/side */
	outb(0x1b, 0x3f5); /* length of GAP 3 */
	outb(0xff, 0x3f5); /* data length (0xff = unused) */
	
	/*
	 * Wait until command is has been finished
	 */
	var_put(f_recal, var_get(f_recal) & ~(1 << 7));
	while (! (var_get(f_recal) & (1 << 7))) {
		asm volatile (
			"sti\n\t"
			"hlt\n\t"
			"cli\n\t"
		);
	}

	/*
	 * Read result
	 */
	var_put(f_stat[0], inb(0x3f5));
	var_put(f_stat[1], inb(0x3f5));
	var_put(f_stat[2], inb(0x3f5));
	var_put(f_stat[3], inb(0x3f5));
	var_put(f_stat[4], inb(0x3f5));
	var_put(f_stat[5], inb(0x3f5));
	var_put(f_stat[6], inb(0x3f5));

	/*
	 * DMA
	 */
	outb(0x02, 0x0a); /* Disable DMA 2. */

	return floppy_err(var_get(f_stat[0]),
			var_get(f_stat[1]), var_get(f_stat[2]));
}

static unsigned char
floppy_write_chs(
	unsigned char drive,
	unsigned char num,
	unsigned char cylinder,
	unsigned char head,
	unsigned char sector,
	unsigned short buffer_seg,
	unsigned short buffer_off
)
{
	unsigned char status;
	unsigned long size;
	unsigned long address;

	if (1 < drive) {
		return 0x01;	/* invalid parameter */
	}

	if (1 < head) {
		return 0x01;	/* invalid parameter */
	}

	size = num * 512 - 1;
	address = ((unsigned long) buffer_seg << 4) + buffer_off;
	if((address & 0xffff) + size > 0xffff) {
		return 0x09;	/* dma boundary */
	}

	/*
	 * Start motor
	 */
	floppy_poweron(drive);
	
	/*
	 * Set up DMA
	 */
	outb(0x06, 0x0a); /* Enable DMA 2. */
	
	outb(0x00, 0x0c); /* Clear byte pointer flip/flop. */
	outb((address >> 0) & 0xff, 0x04); /* Address low byte. */
	outb((address >> 8) & 0xff, 0x04); /* Address high byte. */
	
	outb(0x00, 0x0c); /* Clear byte pointer flip/flop. */
	outb((size >> 0) & 0xff, 0x05); /* Count low byte. */
	outb((size >> 8) & 0xff, 0x05); /* Count high byte. */
	
	/* Mode register: single block, inc addr, no autoinit, write, chan 2 */
	outb((1 << 6) | (0 << 5) | (0 << 4) | (2 << 2) | 2, 0x0b);

	outb((address >> 16) & 0xff, 0x81); /* Page register */

	/*
	 * Wait until DR is ready
	 */
	do {
		status = inb(0x3f4);
	} while ((status & 0xc0) != 0x80);

	/*
	 * Send write command.
	 */
	outb(0xc5, 0x3f5); /* WRITE */
	outb((head << 2) | drive, 0x3f5);
	outb(cylinder, 0x3f5); /* cylinder */
	outb(head, 0x3f5); /* head */
	outb(sector, 0x3f5); /* sector */
	outb(0x02, 0x3f5); /* sectorsize = 0x02 >> 7 */
	outb(18, 0x3f5); /* sectors per track/side */
	outb(0x1b, 0x3f5); /* length of GAP 3 */
	outb(0xff, 0x3f5); /* data length (0xff = unused) */
	
	/*
	 * Wait until command is has been finished
	 */
	var_put(f_recal, var_get(f_recal) & ~(1 << 7));
	while (! (var_get(f_recal) & (1 << 7))) {
		asm volatile (
			"sti\n\t"
			"hlt\n\t"
			"cli\n\t"
		);
	}

	/*
	 * Read result
	 */
	var_put(f_stat[0], inb(0x3f5));
	var_put(f_stat[1], inb(0x3f5));
	var_put(f_stat[2], inb(0x3f5));
	var_put(f_stat[3], inb(0x3f5));
	var_put(f_stat[4], inb(0x3f5));
	var_put(f_stat[5], inb(0x3f5));
	var_put(f_stat[6], inb(0x3f5));
	
	/*
	 * DMA
	 */
	outb(0x02, 0x0a); /* Disable DMA 2. */

	return floppy_err(var_get(f_stat[0]),
			var_get(f_stat[1]), var_get(f_stat[2]));
}

static unsigned char
floppy_verify_chs(
	unsigned char drive,
	unsigned char num,
	unsigned char cylinder,
	unsigned char head,
	unsigned char sector,
	unsigned short buffer_seg,
	unsigned short buffer_off)
{
	return 0x01;
}

#if FD_CHANGELINE_SUPPORT
static unsigned char
floppy_check_changeline(unsigned char drive)
{
	unsigned char status;
	unsigned char result;

	if (2 <= drive) {
		return 0x80;	/* drive not ready or present */
	}

	/* FIXME sand:
	 * not sure, whether this method to figure out
	 * disk change is correct...
	 */

	floppy_poweron(drive);

	if (inb(0x3f7) & 0x80) {
		/*
		 * Wait until DR is ready
		 */
		do {
			status = inb(0x3f4);
		} while ((status & 0xc0) != 0x80);

		/*
		 * Send recalibrate command.
		 */
		outb(0x07, 0x3f5); /* RECALIBRATE */
		outb(drive, 0x3f5);

		/*
		 * Wait until command has been finished
		 */
		var_put(f_recal, var_get(f_recal) & ~(1 << 7));
		while (! (var_get(f_recal) & (1 << 7))) {
			asm volatile (
					"sti\n\t"
					"hlt\n\t"
					"cli\n\t"
				     );
		}

		/*
		 * Wait until DR is ready
		 */
		do {
			status = inb(0x3f4);
		} while ((status & 0xc0) != 0x80);

		/*
		 * Send sense interrupt status command.
		 */
		outb(0x08, 0x3f5); /* SENSE INTERRUPT STATUS */
		outb(drive, 0x3f5);

		/*
		 * Wait until DR is ready
		 */
		do {
			status = inb(0x3f4);
		} while ((status & 0xd0) != 0xd0);

		var_put(f_stat[0], inb(0x3f5));
		var_put(f_stat[1], inb(0x3f5));

		if (var_get(f_stat[1]) == 1
		 && (var_get(f_stat[0]) & 0xf0) == 0x20) {
			if (inb(0x37f) & 0x80) {
				result = 0x80;	/* drive not ready or present */
			} else {
				result = 0x06; /* change line active */
			}
		} else {
			result = 0x80; /* drive not ready or present */
		}
	} else {
		result = 0; /* disk not changed */
	}

	floppy_poweroff();

	return result;
}
#endif /* FD_CHANGELINE_SUPPORT */

static void
outsw(unsigned short port, unsigned short *addr, uint32_t count)
{
	for ( ; 0 < count; count--) {
		outw(*addr++, port);
	}
}

static unsigned char
send_atapi_pc(
	unsigned short unit,
	unsigned short port,
	struct atapi_compacket *command
)
{
	uint8_t status;

	/* Select drive. */
	outb(unit << 4, port + 6);

	/* Wait while busy. */
	do {
		status = inb(port + 0x206);
	} while (status & IDE_BUSY_STAT);

	/* Send packet command. */
	outb(0x00, port + 1);		/* No DMA, no overlay */
	outb(2048 % 256, port + 4);	/* 2048 bytes transfer */
	outb(2048 / 256, port + 5);
	outb(0xa0, port + 7);		/* Packet command */

	/* Wait while busy. */
	do {
		status = inb(port + 0x206);
	} while (status & IDE_BUSY_STAT);
	if (status & IDE_ERR_STAT) {
		/* Use sense command - FIXME MARCEL */
		return 1;
	}
	assert(status & IDE_DRQ_STAT);

	assert(((inb(port + 2) >> 0) & 1) == 1); /* Command */
	assert(((inb(port + 2) >> 1) & 1) == 0); /* Out */
	assert(inb(port + 4) == 2048 % 256);
	assert(inb(port + 5) == 2048 / 256);

	/* Send command packet. */
	outsw(port, (void *) command, sizeof(struct atapi_compacket) / 2);

	/* Wait while busy. */
	do {
		status = inb(port + 0x206);
	} while (status & IDE_BUSY_STAT);
	if (status & IDE_ERR_STAT) {
		return 1;
	}

	return 0;
}

static unsigned char
atapi_request_sense(
	unsigned short unit,
	unsigned short port,
	struct atapi_request_sense *rs
)
{
	unsigned short *buf = (unsigned short *) rs;
	unsigned char command[12];
	unsigned char status;
	unsigned short count;

	for (count = 0; count < sizeof(command) / sizeof(command[0]); count++) {
		command[count] = 0;
	}
	command[0] = ATAPI_REQUEST_SENSE;
	command[4] = sizeof(*rs);

	/*
	 * Send ATAPI command.
	 */
	if (send_atapi_pc(unit, port, (struct atapi_compacket *) command)) {
		return 1;
	}

	/*
	 * Wait for result.
	 */
	do {
		status = inb(port + 0x206);
	} while (status & IDE_BUSY_STAT);
	if (status & IDE_ERR_STAT) {
		return 1;
	}

	/*
	 * Get result.
	 */
	for (count = 0; count < sizeof(*rs) / 2; count++) {
		assert(status & IDE_DRQ_STAT);
		*buf++ = inw(port);
	}

	return 0;
}

unsigned char
read_sector_lba_atapi(
	unsigned char device,
	unsigned short num,
	uint32_t /* long long */ block,
	unsigned short buffer_seg,
	unsigned short buffer_off,
	unsigned int sector_size
	)
{
	unsigned short port;
	unsigned short atapi_num;
	unsigned short start_dummy;
	unsigned short end_dummy;
	unsigned int ret;
	unsigned int count;
	unsigned int sector_count;
	unsigned short data;
	union {
		struct atapi_compacket command;
		struct atapi_request_sense rs;
	} u;
	unsigned char status;

again:	;
	assert(/* 0 <= device / 2 && */ device / 2 < 6);
	assert(/* 0 <= device % 2 && */ device % 2 < 2);

	/* Get port addresses. */
	port = const_get(ide_port_table[device / 2]);

	/*
	 * Send read command.
	 */
	u.command.opcode = ATAPI_READ_SECTOR;
	u.command.reserved0 = 0;
	u.command.reserved1 = 0;
	u.command.reserved2 = 0;
	u.command.reserved3 = 0;
	if (sector_size == 2048) {
		/* CDROM mode */
		u.command.lba = htonl(block);
		start_dummy = 0;
		end_dummy = 0;
		atapi_num = num;

	} else {
		/* floppy emulation mode */
		unsigned long slba;
		unsigned long elba;

		assert(sector_size == 512);

		slba = block;
		elba = block + num;

		u.command.lba = htonl(slba / 4);
		start_dummy = slba % 4;
		if (elba % 4 == 0) {
			end_dummy = 0;
		} else {
			end_dummy = 4 - elba % 4;
		}
		atapi_num = elba / 4 - slba / 4;
		if (elba % 4 != 0) {
			atapi_num++;
		}
	}
	u.command.misc.transfer_length = htons(atapi_num);

	ret = send_atapi_pc(device % 2, port, &u.command);
	if (ret != 0) {
		ret = atapi_request_sense(device % 2, port, &u.rs);
		if (ret != 0) {
			return ret;
		}
		if ((u.rs.sense_key & 0xf) == ATAPI_UNIT_ATTENTION) {
			goto again;
		}
		return 1;
	}

	/* read dummy data */
	for (sector_count = 0; sector_count < start_dummy; sector_count++) {
		do {
			status = inb(port + 0x206);
		} while (status & IDE_BUSY_STAT);
		if (status & IDE_ERR_STAT) {
			return 1;
		}
		assert(status & IDE_DRQ_STAT);
		for (count = 0; count < sector_size / 2; count++) {
			data = inw(port);
		}
	}
	/*
	 * Get wanted sectors.
	 */
	for (sector_count = 0; sector_count < num; sector_count++) {
		do {
			status = inb(port + 0x206);
		} while (status & IDE_BUSY_STAT);
		if (status & IDE_ERR_STAT) {
			return 1;
		}
		assert(status & IDE_DRQ_STAT);
		for (count = 0; count < sector_size / 2; count++) {
			data = inw(port);
			put_word(buffer_seg, buffer_off, data);
			buffer_off += 2;
			if (16 <= buffer_off) {
				buffer_seg++;
				buffer_off -= 16;
			}
		}
	}
	/* read dummy data */
	for (sector_count = 0; sector_count < end_dummy; sector_count++) {
		do {
			status = inb(port + 0x206);
		} while (status & IDE_BUSY_STAT);
		if (status & IDE_ERR_STAT) {
			return 1;
		}
		assert(status & IDE_DRQ_STAT);
		for (count = 0; count < sector_size / 2; count++) {
			data = inw(port);
		}
	}

	return 0;
}

static void
int13_eltorito(struct regs *regs)
{
	struct info {
		unsigned char size;
		unsigned char media;
		unsigned char emulated_drive;
		unsigned char controller_index;
		unsigned long ilba;
		unsigned short device_spec;
		unsigned short buffer_segment;
		unsigned short load_segment;
		unsigned short sector_count;
		unsigned char cylinders;
		unsigned char spt;
		unsigned char heads;
	};

	if (AH == 0x4a		/* ElTorito - Initiate disk emu */
	 || AH == 0x4c		/* ElTorito - Initiate disk emu and boot */
	 || AH == 0x4d) {	/* ElTorito - Return Boot catalog */
		/* FIXME VOSSI */
		assert(0);
		int13_fail(regs, 0x01);

	} else if (AH == 0x4b) {
		/*
		 * ElTorito - Terminate disk emu
		 */
		if (ebda_get(cdemu.media) == 0x00) {
			/* No Emulation */
			/* Nothing to do... */

		} else if (0x01 <= ebda_get(cdemu.media)
			&& ebda_get(cdemu.media) <= 0x03) {
			/* Floppy Emulation */
			/* Decrement number of floppies. */
			uint16_t sys_conf;
			uint8_t num;

			sys_conf = var_get(sys_conf);

			assert((sys_conf >> 0) & 1);
			num = ((sys_conf >> 6) & 0x3) + 1;
			num--;

			if (! num) {
				sys_conf &= ~(1 << 0);
				sys_conf &= ~(3 << 6);
			} else {
				sys_conf |= 1 << 0;
				sys_conf &= ~(3 << 6);
				sys_conf |= (num - 1) << 6;
			}

			var_put(sys_conf, sys_conf);

		} else if (ebda_get(cdemu.media) == 0x04) {
			/* Harddisk */
			uint8_t hd_num;

			hd_num = var_get(hd_num);

			assert(hd_num);
			hd_num--;

			var_put(hd_num, hd_num);

		} else {
			assert(0);
		}

		put_byte(DS, SI + offsetof(struct info, size),
			0x13);
		put_byte(DS, SI + offsetof(struct info, media),
			ebda_get(cdemu.media));
		put_byte(DS, SI + offsetof(struct info, emulated_drive),
			ebda_get(cdemu.emulated_drive));
		put_byte(DS, SI + offsetof(struct info, controller_index),
			ebda_get(cdemu.controller_index));
		put_long(DS, SI + offsetof(struct info, ilba),
			ebda_get(cdemu.ilba));
		put_word(DS, SI + offsetof(struct info, device_spec),
			ebda_get(cdemu.device_spec));
		put_word(DS, SI + offsetof(struct info, buffer_segment),
			ebda_get(cdemu.buffer_segment));
		put_word(DS, SI + offsetof(struct info, load_segment),
			ebda_get(cdemu.load_segment));
		put_word(DS, SI + offsetof(struct info, sector_count),
			ebda_get(cdemu.sector_count));
		put_byte(DS, SI + offsetof(struct info, cylinders),
			ebda_get(cdemu.vdevice.cylinders));
		put_byte(DS, SI + offsetof(struct info, spt),
			ebda_get(cdemu.vdevice.spt));
		put_byte(DS, SI + offsetof(struct info, heads),
			ebda_get(cdemu.vdevice.heads));

		if (AL == 0x00) {
			/* Should be handled accordingly to spec */
			/* FIXME VOSSI */
			ebda_put(cdemu.active, 0);
		}

		var_put(hd_res, 0x00);
		int13_success(regs, 0x00);
	}
}

static void
int13_floppy(struct regs *regs)
{
	unsigned char ret;
#if 0
	assert(/* 0x00 <= DL && */ DL <= 0x01);
#endif

	if (AH == 0x00) {
		/*
		 * Floppy controller reset.
		 */
		unsigned char drive_type;

		if (2 <= DL) {
			int13_fail(regs, 0x01);	/* Invalid param. */
			return;
		}

		drive_type = cmos_get(fd_config);
		if (DL == 0) {
			drive_type >>= 4;
		} else { assert(DL == 1);
			drive_type &= 0xf;
		}

		if (drive_type == 0) {
			int13_fail(regs, 0x80);	/* Not responding. */
			return;
		}

		var_put(f_track[DL], 0);
		int13_success(regs, 0x00);

	} else if (AH == 0x01) {
		/*
		 * Read floppy status.
		 */
		if (var_get(fd_res) == 0) {
			int13_success(regs, 0x00);
		} else {
			int13_fail(regs, var_get(fd_res));
		}

	} else if (AH == 0x02) {
		/*
		 * Read floppy sectors.
		 */
		ret = floppy_read_chs(DL, AL, CH, DH, CL, ES, BX);
		if (ret == 0) {
			int13_success(regs, 0x00);
		} else {
			AL = 0;
			int13_fail(regs, ret);
		}

	} else if (AH == 0x03) {
		/*
		 * Write floppy sectors.
		 */
		ret = floppy_write_chs(DL, AL, CH, DH, CL, ES, BX);
		if (ret == 0) {
			int13_success(regs, 0x00);
		} else {
			AL = 0;
			int13_fail(regs, ret);
		}

	} else if (AH == 0x04) {
		/*
		 * Verify floppy sectors.
		 */
		ret = floppy_verify_chs(DL, AL, CH, DH, CL, ES, BX);
		if (ret == 0) {
			int13_success(regs, 0x00);
		} else {
			AL = 0;
			int13_fail(regs, ret);
		}

	} else if (AH == 0x05) {
		/*
		 * Format disk track.
		 */
		dprintf("int13_floppy: Unknown function %02x\n", AH);
		assert(0);

	} else if (AH == 0x08) {
		/*
		 * Read floppy drive parameters.
		 */
		static CONST struct {
			unsigned char max_track;
			unsigned char sectors;
			unsigned char max_head;

		} floppy_types[] = {
			/* 0 none         */	{ 0, 0, 0 },
			/* 1 360KB, 5.25" */	{ 40-1, 9, 2-1 },
			/* 2 1.2MB, 5.25" */	{ 80-1, 15, 2-1 },
			/* 3 720KB, 3.5"  */	{ 80-1, 9, 2-1 },
			/* 4 1.44MB, 3.5" */	{ 80-1, 18, 2-1 },
			/* 5 2.88MB, 3.5" */	{ 80-1, 36, 2-1 },
			/* 6 160KB, 5.25" */	{ 40-1, 8, 1-1 },
			/* 7 180KB, 5.25" */	{ 40-1, 9, 1-1 },
			/* 8 320KB, 5.25" */	{ 40-1, 8, 2-1 },
			/* 9 unknown      */	{ 0, 0, 0 },
			/* a unknown      */	{ 0, 0, 0 },
			/* b unknown      */	{ 0, 0, 0 },
			/* c unknown      */	{ 0, 0, 0 },
			/* d unknown      */	{ 0, 0, 0 },
			/* e unknown      */	{ 0, 0, 0 },
			/* f unknown      */	{ 0, 0, 0 }
		};

		unsigned char num_floppies;
		unsigned char drive_type;

		num_floppies = 0;
		drive_type = cmos_get(fd_config);
		if ((drive_type >> 0) & 0xf) {
			num_floppies++;
		}
		if ((drive_type >> 4) & 0xf) {
			num_floppies++;
		}

		if (2 <= DL) {
			AL = 0;
			BX = 0;
			CX = 0;
			DX = 0;
			ES = 0;
			DI = 0;
			DL = num_floppies;
			AH = 0x00;	/* no error! */
			F |= 1 << 0;	/* Set carry. */
			return;
		}

		if (DL == 0) {
			drive_type >>= 4;
		} else { assert(DL == 1);
			drive_type &= 0xf;
		}

		BH = 0;
		BL = drive_type;
		AL = 0;
		DL = num_floppies;
		CH = const_get(floppy_types[drive_type].max_track);
		CL = const_get(floppy_types[drive_type].sectors);
		DH = const_get(floppy_types[drive_type].max_head);

		/* Get diskette parameter table stored in INT vector 0x1e. */
		DI = get_word(0x0000, 0x1e * 4 + 0);
		ES = get_word(0x0000, 0x1e * 4 + 2);

		/* Disk status not changed upon success. */
		AH = 0x00;
		F &= ~(1 << 0); /* Clear carry. */

	} else if (AH == 0x15) {
		/*
		 * Read floppy drive type.
		 */
		unsigned char drive_type;

		if (2 <= DL) {
			/* var_put(fmot_stat, ...);	FIXME VOSSI */
			int13_fail(regs, 0x01);
			return;
		}

		drive_type = cmos_get(fd_config);
		if (DL == 0) {
			drive_type >>= 4;
		} else { assert(DL == 1);
			drive_type &= 0xf;
		}
		if (drive_type == 0) {
			int13_success(regs, 0x00);	/* Drive not present. */
		} else {
							/* Drive present. */
#if FD_CHANGELINE_SUPPORT
			int13_success(regs, 0x02);	/* Does support */
							/* change line. */
#else
			int13_success(regs, 0x01);	/* Does not support */
							/* change line. */
#endif
		}
	} else if (AH == 0x16) {
		/*
		 * Get floppy change line status.
		 */
#if FD_CHANGELINE_SUPPORT
		ret = floppy_check_changeline(DL);
		if(ret == 0) {
			/* disk not changed: return successfully */
			int13_success(regs,0x00);
		} else {
			/* other cases:
			 *  - disk changed
			 *  - no disk present,
			 *  - drive not ready,
			 *  - drive not present,
			 *  - ...
			 * => return as if failed
			 */
			int13_fail(regs,ret);
		}
#else
		dprintf("int13_floppy: Unknown function %02x\n", AH);
		assert(0);
#endif
	} else if (AH == 0x17) {
		/*
		 * Set floppy type for format (old).
		 */
		dprintf("int13_floppy: Unknown function %02x\n", AH);
		assert(0);

	} else if (AH == 0x18) {
		/*
		 * Set floppy type for format (new).
		 * AH:			0x18
		 * DL:			drive
		 * CH:			cylinder number (bits 0-7)
		 * CL (bit 6-7):	cylinder number (bits 8-9)
		 * CL (bit 0-5):	sectors per track
		 */
		dprintf("int13_floppy: Set floppy type called (%d,%d).\n",
				(((CL >> 6) & 3) << 8) | CH, (CL >> 0) & 0x3f);
		int13_success(regs, 0x00);	/* FIXME VOSSI */

	} else if (AH == 0x41) {
		/*
		 * BIOS Enhanced Disk Drive call:
		 * "Check Extensions Present".
		 */
		/* Not supported for floppy disks. FIXME VOSSI */
		int13_fail(regs, 0x01);

	} else {
		dprintf("int13_floppy: Drive 0x%02x: Unknown function %02x\n",
				DL, AH);
		int13_fail(regs, 0x01);
	}
}

static void
int13_harddisk(struct regs *regs)
{
	unsigned char device;

	if (DL < 0x80 || 0x80 + MAX_BIOS_HDS <= DL) {
		var_put(hd_res, 0x01);
		AH = 0x01;
		F |= 1 << 0; /* Set carry. */
		return;
	}
	device = ebda_get(hdidmap[DL - 0x80]);
	if (MAX_BIOS_HDS <= device) {
		var_put(hd_res, 0x01);
		AH = 0x01;
		F |= 1 << 0; /* Set carry. */
		return;
	}

	/* Clear completion flag. */
	var_put(hd_irq, 0);

	if (AH == 0x00) {
		/*
		 * Reset disk controller.
		 */
		/* ata_reset(device); */	/* FIXME VOSSI */
		var_put(hd_res, 0x00);
		AH = 0x00;
		F &= ~(1 << 0); /* Clear carry. */

	} else if (AH == 0x02
		|| AH == 0x03
		|| AH == 0x04) {
		/*
		 * Read disk sectors.
		 * Write disk sectors.
		 * Verify disk sectors.
		 */
		unsigned char count;
		unsigned short cylinder;
		unsigned char sector;
		unsigned char head;
		unsigned short segment;
		unsigned short offset;
		unsigned char status;

		count = AL;
		cylinder = ((unsigned short) (CL & 0xc0) << 2) | CH;
		sector = CL & 0x3f;
		head = DH;

		segment = ES;
		offset = BX;

		/* Sanity check on count. */
		if (128 < count || count == 0) {
			var_put(hd_res, 0x01);
			AH = 0x01;
			F |= 1 << 0; /* Set carry. */
			return;
		}

		/* Sanity check on C/H/S. */
		if (ebda_get(devices[device].lchs_cylinders) <= cylinder
		 || ebda_get(devices[device].lchs_heads) <= head
		 || ebda_get(devices[device].lchs_spt) < sector) {
			var_put(hd_res, 0x01);
			AH = 0x01;
			F |= 1 << 0; /* Set carry. */
			return;
		}

		if (AH == 0x02) {
			status = ide_harddisk_read_chs(device,
					count, cylinder, head, sector,
					segment, offset);
		} else if (AH == 0x03) {
			status = ide_harddisk_write_chs(device,
					count, cylinder, head, sector,
					segment, offset);
		} else {
			/* Verify */
			status = 0; /* FIXME */
		}
		
		/* Set nb of sector transferred. */
		/* FIXME VOSSI */

		if (status != 0) {
			var_put(hd_res, 0x0c);
			AH = 0x0c;
			F |= 1 << 0; /* Set carry. */
			return;
		}

		var_put(hd_res, 0x00);
		AH = 0x00;
		F &= ~(1 << 0); /* Clear carry. */

	} else if (AH == 0x05) {
		/*
		 * Format disk track.
		 */
		/* FIXME VOSSI */
		var_put(hd_res, 0x00);
		AH = 0x00;
		F &= ~(1 << 0); /* Clear carry. */

	} else if (AH == 0x08) {
		/*
		 * Read disk drive parameters.
		 */
		unsigned short nlc;
		unsigned short nlh;
		unsigned short nlspt;
		unsigned short count;

		nlc = ebda_get(devices[device].lchs_cylinders);
		if (1024 < nlc) {
			nlc = 1024;	/* FIXME VOSSI */
		}
		nlh = ebda_get(devices[device].lchs_heads);
		nlspt = ebda_get(devices[device].lchs_spt);
		count = ebda_get(harddrives);

		nlc = nlc - 2; /* 0 based , last sector not used */

		AL = 0;
		CH = nlc & 0xff;
		CL = ((nlc >> 8) << 6) | (nlspt & 0x3f);
		DH = nlh - 1;
		DL = count;	/* FIXME returns 0, 1, or n hard drives */

		var_put(hd_res, 0x00);
		AH = 0x00;
		F &= ~(1 << 0); /* Clear carry. */

	} else if (AH == 0x10) {
		/*
		 * Check drive ready.
		 */
		/* FIXME */

		var_put(hd_res, 0x00);
		AH = 0x00;
		F &= ~(1 << 0); /* Clear carry. */

	} else if (AH == 0x11) {
		/*
		 * Recalibrate drive.
		 */
		/* FIXME */

		var_put(hd_res, 0x00);
		AH = 0x00;
		F &= ~(1 << 0); /* Clear carry. */

	} else if (AH == 0x15) {
		/*
		 * Read disk drive size.
		 */
		unsigned short npc;
		unsigned short nph;
		unsigned short npspt;
		unsigned long lba;

		/* Get physical geometry from table. */
		/* FIXME VOSSI */
		npc = ebda_get(devices[device].pchs_cylinders);
		nph = ebda_get(devices[device].pchs_heads);
		npspt = ebda_get(devices[device].pchs_spt);

		/* Compute sector count seen by int13. */
		lba = (unsigned long) (npc - 1)
			* (unsigned long) nph
			* (unsigned long) npspt;

		CX = (lba >> 16) & 0xffff;
		DX = (lba >>  0) & 0xffff;

		var_put(hd_res, 0x00);
		AH = 0x03; /* Type: 3 (Harddisk) */
		F &= ~(1 << 0); /* Clear carry. */

	} else if (AH == 0x41) {
		/*
		 * BIOS Enhanced Disk Drive call:
		 * "Check Extensions Present".
		 */
		BX = 0xaa55;	/* Install check. */
		CX = 0x0007;	/* Options supported. */

		var_put(hd_res, 0x00);
		AH = 0x30; /* Version 3.0 */
		F &= ~(1 << 0); /* Clear carry. */

	} else if (AH == 0x42
		|| AH == 0x43
		|| AH == 0x44
		|| AH == 0x47) {
		/*
		 * Extended read.
		 * Extended write.
		 * Extended verify.
		 * Extended seek.
		 */
		unsigned short count;
		unsigned short segment;
		unsigned short offset;
#if 0
		unsigned long long lba;
#else
		unsigned long lba;
#endif
		unsigned char status;


		count = get_word(DS, SI + 2);
		segment = get_word(DS, SI + 6);
		offset = get_word(DS, SI + 4);
#if 0
		lba = get_qword(DS, SI + 8);
#else
		lba = get_long(DS, SI + 8);
#endif

		/* Sanity check on lba. */
		if (ebda_get(devices[device].sectors) <= lba) {
			var_put(hd_res, 0x01);
			AH = 0x01;
			F |= 1 << 0; /* Set carry. */
			return;
		}

		if (AH == 0x44 || AH == 0x47) {
			/* Verify or seek. */
			var_put(hd_res, 0x00);
			AH = 0x00;
			F &= ~(1 << 0); /* Clear carry. */
			return;
		}

		if (AH == 0x42) {
			/* Read */
			status = ide_harddisk_read_lba(device / 2, device % 2,
					count, lba, segment, offset);
		} else { assert(AH == 0x43);
			/* Write */
			status = ide_harddisk_write_lba(device / 2, device % 2,
					count, lba, segment, offset);
		}

		if (status != 0) {
			var_put(hd_res, 0x0c);
			AH = 0x0c;
			F |= 1 << 0; /* Set carry. */
			return;
		}

		/* FIXME VOSSI */
		put_word(DS, SI + 2, count);

		var_put(hd_res, 0x00);
		AH = 0x00;
		F &= ~(1 << 0); /* Clear carry. */

	} else if (AH == 0x45
		|| AH == 0x49) {
		/*
		 * IBM/MS lock/unlock drive.
		 * IBM/MS extended media change.
		 */
		/* All these functions return SUCCESS. */
		var_put(hd_res, 0x00);
		AH = 0x00;
		F &= ~(1 << 0); /* Clear carry. */

	} else if (AH == 0x46) {
		/*
		 * IBM/MS eject media.
		 */
		/* This function returns FAILURE. */
		var_put(hd_res, 0xb2);
		AH = 0xb2;
		F |= 1 << 0; /* Set carry. */

	} else if (AH == 0x48) {
		/*
		 * BIOS Enhanced Disk Drive call:
		 * "Get Device Parameters".
		 */
		struct info {
			unsigned short size;
			unsigned short flags;
			unsigned long cyls;
			unsigned long heads;
			unsigned long secs;
			unsigned long total_low;	/* Should be long long */
			unsigned long total_high;
			unsigned short bytes_per_sec;
			/* ... */
		};
		unsigned short size;

		size = get_word(DS, SI + offsetof(struct info, size));

		if (size < 0x1a) {
			/* Buffer is too small. */
			var_put(hd_res, 0x01);
			AH = 0x01;
			F |= 1 << 0; /* Set carry. */
			return;
		}

		if (0x1a <= size) {
			/* EDD 1.x */
			unsigned long cyls;
			unsigned long heads;
			unsigned long secs;
			unsigned long /* long */ total;	/* FIXME MARCEL */

			cyls = ebda_get(devices[device].pchs_cylinders);
			heads = ebda_get(devices[device].pchs_heads);
			secs = ebda_get(devices[device].pchs_spt);
			total = cyls * heads * secs;

			put_word(DS, SI + offsetof(struct info, size), 0x1a);
			put_word(DS, SI + offsetof(struct info, flags), 0x02);
			put_long(DS, SI + offsetof(struct info, cyls), cyls);
			put_long(DS, SI + offsetof(struct info, heads), heads);
			put_long(DS, SI + offsetof(struct info, secs), secs);
			put_long(DS, SI + offsetof(struct info, total_low), total);
			put_long(DS, SI + offsetof(struct info, total_high), 0L); /* FIXME MARCEL */
			put_word(DS, SI + offsetof(struct info, bytes_per_sec), 512);
		}
		if (0x1e <= size) {
			/* EDD 2.x */
			/* put_word(DS, SI + offsetof(struct info, size), 0x1e); */
			put_word(DS, SI + offsetof(struct info, size), 0x1a);

			/* Not supported yet. FIXME VOSSI */
		}
		if (0x42 <= size) {
			/* EDD 3.x */
			/* Not supported yet. FIXME VOSSI */
		}

		var_put(hd_res, 0x00);
		AH = 0x00;
		F &= ~(1 << 0); /* Clear carry. */

	} else if (AH == 0x4e) {
		/*
		 * Set hardware configuration.
		 */
		assert(0);	/* FIXME VOSSI */

	} else if (AH == 0x09
		|| AH == 0x0c
		|| AH == 0x0d
		|| AH == 0x11
		|| AH == 0x14) {
		/*
		 * Initialize drive parameters.
		 * Seek to specified cylinder.
		 * Alternate disk reset.
		 * Recalibrate.
		 * Controller internal diagnostic.
		 */
		/* All these functions return SUCCESS. */
		var_put(hd_res, 0x00);
		AH = 0x00;
		F &= ~(1 << 0); /* Clear carry. */

	} else if (AH == 0x0a
		|| AH == 0x0b
		|| AH == 0x18
		|| AH == 0x50) {
		/*
		 * Read disk sectors with ECC.
		 * Write disk sectors with ECC.
		 * Set media type for format.
		 * IBM/MS send packet command.
		 */
		/* All these functions return UNSUPPORTED. */
		var_put(hd_res, 0x01);
		AH = 0x01;
		F |= 1 << 0; /* Set carry. */

	} else {
		dprintf("int13_harddisk: AH=0x%02x\n", AH);
		/* Return UNSUPPORTED. */
		var_put(hd_res, 0x01);
		AH = 0x01;
		F |= 1 << 0; /* Set carry. */
	}
}

static void
int13_cdrom(struct regs *regs)
{
	unsigned char device;

	if (DL < 0xe0 || 0xe0 + MAX_BIOS_HDS <= DL) {
		var_put(hd_res, 0x01);
		int13_fail(regs, 0x01);
		return;
	}
	device = ebda_get(cdidmap[DL - 0xe0]);
	if (device == MAX_BIOS_HDS) {
		var_put(hd_res, 0x01);
		int13_fail(regs, 0x01);
		return;
	}

	if (AH == 0x00
	 || AH == 0x09
	 || AH == 0x0c
	 || AH == 0x0d
	 || AH == 0x10
	 || AH == 0x11
	 || AH == 0x14
	 || AH == 0x16) {
		/*
		 * Disk controller reset.
		 * Initialize drive parameters.
		 * Seek to specified cylinder.
		 * Alternate disk reset.
		 * Check drive ready.
		 * Recalibrate.
		 * Controller internal diagnostic.
		 * Detect disk change.
		 */
		/* All these functions return SUCCESS. */
		var_put(hd_res, 0x00);
		int13_success(regs, 0x00);

	} else if (AH == 0x03
		|| AH == 0x05
		|| AH == 0x43) {
		/*
		 * Write disk sectors.
		 * Format disk track.
		 * Extended write.
		 */
		/* All these functions return DISK WRITE-PROTECTED. */
		var_put(hd_res, 0x03);
		int13_success(regs, 0x03);

	} else if (AH == 0x01) {
		/*
		 * Read disk status.
		 */
		unsigned char status;

		status = var_get(hd_res);
		var_put(hd_res, 0x00);
		if (status) {
			int13_fail(regs, status);
		} else {
			int13_success(regs, status);
		}

	} else if (AH == 0x015) {
		/*
		 * Read disk drive size.
		 */
		var_put(hd_res, 0x02);
		int13_fail(regs, 0x02);

	} else if (AH == 0x41) {
		/*
		 * BIOS Enhanced Disk Drive call:
		 * "Check Extensions Present".
		 */
		BX = 0xaa55;		/* Install check. */
		CX = 0x0007;		/* Ext disk access, removable and EDD.*/
		var_put(hd_res, 0x00);
		int13_success(regs, 0x01);	/* Version 1.0 */

	} else if (AH == 0x42
		|| AH == 0x44
		|| AH == 0x47) {
		/*
		 * Extended read.
		 * Extended verify sectors.
		 * Extended seek.
		 */
		unsigned short count;
		unsigned short segment;
		unsigned short offset;
#if 0	/* FIXME VOSSI */
		unsigned long long lba;
#else
		unsigned long lba;
#endif
		unsigned char status;

		count = get_word(DS, SI + offsetof(struct Int13Ext, count));
		segment = get_word(DS, SI + offsetof(struct Int13Ext, segment));
		offset = get_word(DS, SI + offsetof(struct Int13Ext, offset));

#if 0	/* FIXME VOSSI */
		lba = get_longlong(DS, SI + offsetof(struct Int13Ext, lba));
#else
		lba = get_long(DS, SI + offsetof(struct Int13Ext, lba));
#endif

		if (AH == 0x44
		 || AH == 0x47) {
			/* Verify or seek. */
			var_put(hd_res, 0x00);
			int13_success(regs, 0x00);
			return;
		}

		status = read_sector_lba_atapi(device,
				count, lba, segment, offset, 2048);
		count = (status == 0) ? count : 0;	/* FIXME VOSSI */
		put_word(DS, SI + offsetof(struct Int13Ext, count), count);
		if (status != 0) {
			var_put(hd_res, 0x0c);
			int13_fail(regs, 0x0c);
			return;
		}

		var_put(hd_res, 0x00);
		int13_success(regs, 0x00);

	} else if (AH == 0x45) {
		/*
		 * Lock/unlock drive.
		 */
		assert(0);	/* FIXME VOSSI */

	} else if (AH == 0x46) {
		/*
		 * Eject media.
		 */
		assert(0);	/* FIXME VOSSI */

	} else if (AH == 0x48) {
		/*
		 * BIOS Enhanced Disk Drive call:
		 * "Get Device Parameters".
		 */
		unsigned short size;

		size = get_word(DS, SI + offsetof(struct Int13DPT, size));

		if (size < 0x1a) {
			/* Buffer is too small. */
			var_put(hd_res, 0x01);
			int13_fail(regs, 0x01);
			return;
		}
		if (0x1a <= size) {
			unsigned short blksize;

			blksize = 2048;	/* FIXME VOSSI */

			put_word(DS, SI + offsetof(struct Int13DPT, size),
					0x1a);

			/* removable, media change, lockable, max values */
			put_word(DS, SI + offsetof(struct Int13DPT, flags),
					0x74);

			put_long(DS, SI + offsetof(struct Int13DPT, cyls),
					0xffffffff);
			put_long(DS, SI + offsetof(struct Int13DPT, heads),
					0xffffffff);
			put_long(DS, SI + offsetof(struct Int13DPT, secs),
					0xffffffff);
			put_long(DS, SI + offsetof(struct Int13DPT, total_low),
					0xffffffff);
			put_long(DS, SI + offsetof(struct Int13DPT, total_high),
					0xffffffff);
			put_word(DS, SI + offsetof(struct Int13DPT, bytes_per_sec),
					blksize);
		}
		if (0x1e <= size) {
			/* EDD 2.x */
			put_word(DS, SI + offsetof(struct Int13DPT, size), 0x1e);

			/* FIXME VOSSI */
		}
		if (0x42 <= size) {
			/* EDD 3.x */
			/* Not supported yet. FIXME VOSSI */
		}

		var_put(hd_res, 0x00);
		int13_success(regs, 0x00);

	} else if (AH == 0x49) {
		/*
		 * Extended media change.
		 */
		/* Always send changed?? */
		var_put(hd_res, 0x06);
		int13_fail(regs, 0x06);

	} else if (AH == 0x4e) {
		/*
		 * Set hardware configuration.
		 */
		/* DMA, prefetch, PIO maximum not supported. */
		if (AL == 0x01
		 || AL == 0x03
		 || AL == 0x04
		 || AL == 0x06) {
			var_put(hd_res, 0x00);
			int13_success(regs, 0x00);
		} else {
			var_put(hd_res, 0x01);
			int13_success(regs, 0x01);
		}

	} else if (AH == 0x02
		|| AH == 0x04
		|| AH == 0x08
		|| AH == 0x0a
		|| AH == 0x0b
		|| AH == 0x18
		|| AH == 0x50) {
		/*
		 * Read sectors.
		 * Verify sectors.
		 * Read disk drive parameters.
		 * Read disk sectors with ECC.
		 * Write disk sectors with ECC.
		 * Set media type for format.
		 * ? - Send packet command.
		 */
		/* All these functions return UNIMPLEMENTED. */
		var_put(hd_res, 0x01);
		int13_fail(regs, 0x01);

	} else {
		dprintf("int13_cdrom: AH=0x%02x\n", AH);
		/* Return UNSUPPORTED. */
		var_put(hd_res, 0x01);
		int13_fail(regs, 0x01);
	}
}

static void
int13_cdemu(struct regs *regs)
{
	unsigned char device;

	/* Recompute the device number. */
	device  = ebda_get(cdemu.controller_index) * 2;
	device += ebda_get(cdemu.device_spec);

	var_put(hd_res, 0x00);

	/*
	 * Basic checks: emulation should be active, DL should equal the
	 * emulated drive.
	 */
	if (ebda_get(cdemu.active) == 0
	 || ebda_get(cdemu.emulated_drive) != DL) {
		assert(0);
	}

	if (AH == 0x00		/* Disk controller reset. */
	 || AH == 0x09		/* Initialize drive parameters. */
	 || AH == 0x0c		/* Seek to specified cylinder. */
	 || AH == 0x0d		/* Alternate disk reset. */
	 || AH == 0x10		/* Check drive ready. */
	 || AH == 0x11		/* Recalibrate. */
	 || AH == 0x14		/* Controller internal diagnostic. */
	 || AH == 0x16) {	/* Detect disk change. */
		/* All these functions return SUCCESS. */
		var_put(hd_res, 0x00);
		int13_success(regs, 0x00);

	} else if (AH == 0x03	/* Write disk sectors. */
		|| AH == 0x05) {/* Format disk track. */
		/* All these functions return DISK WRITE-PROTECTED. */
		var_put(hd_res, 0x03);
		int13_fail(regs, 0x03);
		
	} else if (AH == 0x01) {
		/* Read disk status. */
		unsigned char status;

		status = var_get(hd_res);

		var_put(hd_res, 0x00);
		if (status) {
			int13_fail(regs, status);
		} else {
			int13_success(regs, status);
		}

	} else if (AH == 0x02	/* Read disk sectors. */
		|| AH == 0x04) {/* Verify disk sectors. */
		unsigned short vspt;
		unsigned short vcylinders;
		unsigned short vheads;
		unsigned short sector;
		unsigned short cylinder;
		unsigned short head;
		unsigned short nbsectors;
		unsigned short segment;
		unsigned short offset;
		unsigned long ilba;
		unsigned long vlba;
		unsigned long slba;
		unsigned long before;
		unsigned long elba;
		unsigned char status;

		vspt = ebda_get(cdemu.vdevice.spt);
		vcylinders = ebda_get(cdemu.vdevice.cylinders);
		vheads = ebda_get(cdemu.vdevice.heads);

		ilba = ebda_get(cdemu.ilba);

		sector = CL & 0x3f;
		cylinder = (((unsigned short) CL & 0xc0) << 2) | CH;
		head = DH;
		nbsectors = AL;

		/* No sector to read? */
		if (nbsectors == 0) {
			var_put(hd_res, 0x00);
			int13_success(regs, 0x00);
			return;
		}

		/* Sanity checks. SCO OpenServer needs this! */
		if (vspt < sector
		 || vcylinders <= cylinder
		 || vheads <= head) {
			var_put(hd_res, 0x01);
			int13_fail(regs, 0x01);
			return;
		}

		if (AH == 0x04) {
			/* Verify disk sectors. */
			var_put(hd_res, 0x00);
			int13_success(regs, 0x00);
			return;
		}

		segment = ES + (BX / 16);
		offset = BX % 16;

		/* Calculate the virtual lba inside the image. */
		vlba = ((((unsigned long) cylinder * (unsigned long) vheads)
			+ (unsigned long) head) * (unsigned long) vspt)
			+ (unsigned long) sector - 1;

		/* In advance so we don't loose the count. */
		AL = nbsectors;

		/* Start lba on CD. */
		slba = (unsigned long) vlba / 4;
		before = (unsigned long) vlba % 4;

		/* End lba on CD. */
		elba = (unsigned long) (vlba + nbsectors - 1) / 4;

		/* Read command. */
		status = read_sector_lba_atapi(device,
				nbsectors, ilba * 4 + vlba,
				segment, offset, 512);
		if (status != 0) {
			AL = 0;
			var_put(hd_res, 0x02);
			int13_fail(regs, 0x02);
			return;
		}

		var_put(hd_res, 0x00);
		int13_success(regs, 0x00);

	} else if (AH == 0x08) {
		/* Read disk drive parameters. */
		unsigned short vspt;
		unsigned short vcylinders;
		unsigned short vheads;

		vspt = ebda_get(cdemu.vdevice.spt);
		vcylinders = ebda_get(cdemu.vdevice.cylinders) - 1;
		vheads = ebda_get(cdemu.vdevice.heads) - 1;

		AL = 0x00;
		BL = 0x00;
		CH = vcylinders & 0xff;
		CL = ((vcylinders >> 2) & 0xc0) | (vspt & 0x3f);
		DH = vheads;
		DL = 0x02;	/* FIXME ElTorito Various. should send the real count of drives 1 or 2 */
				/* FIXME ElTorito Harddisk. should send the HD count */
		if (ebda_get(cdemu.media) == 0x01) {
			BL = 0x02;
		} else if (ebda_get(cdemu.media) == 0x02) {
			BL = 0x04;
		} else if (ebda_get(cdemu.media) == 0x03) {
			BL = 0x06;
		}

		/* Get diskette parameter table stored in INT vector 0x1e. */
		DI = get_word(0x0000, 0x1e * 4 + 0);
		ES = get_word(0x0000, 0x1e * 4 + 2);

		var_put(hd_res, 0x00);
		int13_success(regs, 0x00);

	} else if (AH == 0x15) {
		/* Identify drive. */
		/* What to do...? - FIXME VOSSI */
		var_put(hd_res, 0x00);
		int13_success(regs, 0x03);

	} else if (AH == 0x0a	/* Read disk sectors with ECC */
		|| AH == 0x0b	/* Write disk sectors with ECC */
		|| AH == 0x18	/* Set media type for format */
		|| AH == 0x41	/* IBM/MS installation check */
		|| AH == 0x42	/* IBM/MS extended read */
		|| AH == 0x43	/* IBM/MS extended write */
		|| AH == 0x44	/* IBM/MS verify sectors */
		|| AH == 0x45	/* IBM/MS lock/unlock drive */
		|| AH == 0x46	/* IBM/MS eject media */
		|| AH == 0x47	/* IBM/MS extended seek */
		|| AH == 0x48	/* IBM/MS get drive parameters */
		|| AH == 0x49	/* IBM/MS extended media change */
		|| AH == 0x4e	/* ? - set hardware configuration */
		|| AH == 0x50	/* ? - send packet command */
		|| 1) {		/* default */
		dprintf("int13_cdemu: AH=0x%02x\n", AH);
		/* All these functions return UNIMPLEMENTED. */
		var_put(hd_res, 0x01);
		int13_fail(regs, 0x01);
	}
}

C_ENTRY void
bios_13_xxxx(struct regs *regs)
{
	if (0x4a <= AH && AH <= 0x4d) {
		/*
		 * El-torito functions.
		 */
		int13_eltorito(regs);

	} else if (ebda_get(cdemu.active)) {
		if (DL == ebda_get(cdemu.emulated_drive)) {
			/*
			 * Emulated drive.
			 */
			int13_cdemu(regs);

		} else if ((DL & 0xe0) == ebda_get(cdemu.emulated_drive)) {
			/*
			 * Drive in same class as emulated drive.
			 * -> change drive number
			 */
			DL--;

			/* int 13h,08h is special because it returns
			 * the number of drives in DL, so we mustn't
			 * change it before returning from this handler */
			if(AH == 0x08) goto int13_legacy;

			if (DL < 0x80) {
				int13_floppy(regs);
			} else if (DL < 0xe0) {
				int13_harddisk(regs);
			} else {
				int13_cdrom(regs);
			}

			/* change back drive number as some software
			 * seems to rely on it */
			DL++;
		} else {
			/*
			 * Unaffected drive.
			 */
			goto int13_legacy;
		}
	} else {
int13_legacy:
		/*
		 * Standard int13 functions.
		 */
		if (DL < 0x80) {
			int13_floppy(regs);
		} else if (DL < 0xe0) {
			int13_harddisk(regs);
		} else {
			int13_cdrom(regs);
		}
	}
}

C_ENTRY void
bios_0e(struct regs *regs)
{
	var_put(f_recal, var_get(f_recal) | (1 << 7));

	eoi();

#if 0
	asm volatile (
		"pushw %ax\n"
		"movb $0x91, %ah\n" /* Signal end-of-operation. */
		"movb $0x01, %al\n" /* Device type is floppy. */
		"int $0x15\n"
		"popw %ax\n"
	);
#endif
}

#endif /* RUNTIME_RM */
/* ==================== REAL-MODE INIT ==================== */
#ifdef INIT_RM

CODE16;

#include "assert.h"
#include "stdio.h"
#include "string.h"
#include "in.h"
#include "io.h"
#include "cmos.h"
#include "var.h"
#include "el_torito.h"
#include "ptrace.h"
#include "disk.h"
#include "const.h"
#include "video.h"

/* cut && paste of umide.h */
/* structure returned by HDIO_GET_IDENTITY, as per ANSI ATA2 rev.2f spec */
struct hd_driveid {
	unsigned short config;	/* lots of obsolete bit flags */
	unsigned short cyls;	/* "physical" cyls */
	unsigned short reserved2;	/* reserved (word 2) */
	unsigned short heads;	/* "physical" heads */
	unsigned short track_bytes;	/* unformatted bytes per track */
	unsigned short sector_bytes;	/* unformatted bytes per sector */
	unsigned short sectors;	/* "physical" sectors per track */
	unsigned short vendor0;	/* vendor unique */
	unsigned short vendor1;	/* vendor unique */
	unsigned short vendor2;	/* vendor unique */
	unsigned char serial_no[20];	/* 0 = not_specified */
	unsigned short buf_type;
	unsigned short buf_size;    /* 512 byte increments; 0 = not_specified */
	unsigned short ecc_bytes;   /* for r/w long cmds; 0 = not_specified */
	unsigned char fw_rev[8];	/* 0 = not_specified */
	unsigned char model[40];	/* 0 = not_specified */
	unsigned char max_multsect;	/* 0=not_implemented */
	unsigned char vendor3;		/* vendor unique */
	unsigned short dword_io;	/* 0=not_implemented; 1=implemented */
	unsigned char vendor4;		/* vendor unique */
	unsigned char capability;    /* bits 0:DMA 1:LBA 2:IORDYsw 3:IORDYsup */
	unsigned short reserved50;	/* reserved (word 50) */
	unsigned char vendor5;		/* vendor unique */
	unsigned char tPIO;		/* 0=slow, 1=medium, 2=fast */
	unsigned char vendor6;		/* vendor unique */
	unsigned char tDMA;		/* 0=slow, 1=medium, 2=fast */
	unsigned short field_valid;	/* bits 0:cur_ok 1:eide_ok */
	unsigned short cur_cyls;	/* logical cylinders */
	unsigned short cur_heads;	/* logical heads */
	unsigned short cur_sectors;	/* logical sectors per track */
	unsigned short cur_capacity0;	/* logical total sectors on drive */
	unsigned short cur_capacity1;	/*  (2 words, misaligned int)     */
	unsigned char multsect;		/* current multiple sector count */
	unsigned char multsect_valid;	/* when (bit0==1) multsect is ok */
	uint32_t lba_capacity;		/* total number of sectors */
	unsigned short dma_1word;	/* single-word dma info */
	unsigned short dma_mword;	/* multiple-word dma info */
	unsigned short eide_pio_modes;	/* bits 0:mode3 1:mode4 */
	unsigned short eide_dma_min;	/* min mword dma cycle time (ns) */
	unsigned short eide_dma_time;/* recommended mword dma cycle time (ns) */
	unsigned short eide_pio;	/* min cycle time (ns), no IORDY  */
	unsigned short eide_pio_iordy;	/* min cycle time (ns), with IORDY */
	unsigned short words69_70[2];	/* reserved words 69-70 */
	/* HDIO_GET_IDENTITY currently returns only words 0 through 70 */
	unsigned short words71_74[4];	/* reserved words 71-74 */
	unsigned short queue_depth;	/*  */
	unsigned short words76_79[4];	/* reserved words 76-79 */
	unsigned short major_rev_num;	/*  */
	unsigned short minor_rev_num;	/*  */
	/* bits 0:Smart 1:Security 2:Removable 3:PM */
	unsigned short command_set_1;
	unsigned short command_set_2;	/* bits 14:Smart Enabled 13:0 zero */
	unsigned short cfsse;	/* command set-feature supported extensions */
	unsigned short cfs_enable_1;	/* command set-feature enabled */
	unsigned short cfs_enable_2;	/* command set-feature enabled */
	unsigned short csf_default;	/* command set-feature default */
	unsigned short dma_ultra;	/*  */
	unsigned short word89;	/* reserved (word 89) */
	unsigned short word90;	/* reserved (word 90) */
	unsigned short CurAPMvalues;	/* current APM values */
	unsigned short word92;	/* reserved (word 92) */
	unsigned short hw_config;	/* hardware config */
	unsigned short words94_125[32];	/* reserved words 94-125 */
	unsigned short last_lun;	/* reserved (word 126) */
	unsigned short word127;	/* reserved (word 127) */
	unsigned short dlf;	/* device lock function
				 * 15:9        reserved
				 * 8   security level 1:max 0:high
				 * 7:6 reserved
				 * 5   enhanced erase
				 * 4   expire
				 * 3   frozen
				 * 2   locked
				 * 1   en/disabled
				 * 0   capability
				 */
	unsigned short csfo;	/* current set features options
				 * 15:4        reserved
				 * 3   auto reassign
				 * 2   reverting
				 * 1   read-look-ahead
				 * 0   write cache
				 */
	unsigned short words130_155[26]; /* reserved vendor words 130-155 */
	unsigned short word156;
	unsigned short words157_159[3];	 /* reserved vendor words 157-159 */
	unsigned short words160_255[95]; /* reserved words 160-255 */
};


static unsigned char
get_identify_ide(
	unsigned short cmd, /* ATA COMMAND */
	unsigned char contr,
	unsigned char drive,
	unsigned short *buf
)
{
	uint16_t port;
	uint8_t status;
	unsigned int i;

	assert(cmd == WIN_IDENTIFY || cmd == WIN_PIDENTIFY);
	assert(/* 0 <= contr && */ contr < 6);
	assert(/* 0 <= drive && */ drive < 2);

	port = const_get(ide_port_table[contr]);

	/* Wait while busy. */
	for (i = 0; ; i++) {
		if (i == 0xffff) {
			return 1;
		}
		status = inb(port + 0x206);
		if (! (status & IDE_BUSY_STAT)) {
			break;
		}
	}

	/* Send command. */
	outb(drive << 4, port + 6);	/* unit */
	outb(cmd, port + 7);		/* command */

	/* Wait while controller busy. */
	do {
		status = inb(port + 0x206);
	} while (status & IDE_BUSY_STAT);

	if (! (status & IDE_DRQ_STAT)
	 || (status & IDE_ERR_STAT)) {
		/* Strange response -> command not recognized. */
		return 1;
	}

	/* Read info. */
	for (i = 0; i < 512 / sizeof(unsigned short); i++) {
		buf[i] = inw(port);
	}

	return 0;
}

static uint8_t
recalibrate(unsigned char contr, unsigned char drive)
{
	uint16_t port;
	uint8_t status;

	assert(/* 0 <= contr && */ contr < 6);
	assert(/* 0 <= drive && */ drive < 2);

	port = const_get(ide_port_table[contr]);

	/* Send command. */
	outb(0, port + 1);		/* Features */
	outb(1, port + 2);		/* #Sectors */
	outb(0, port + 3);		/* Sector Number */
	outb(0, port + 4);		/* Cylinder Low */
	outb(0, port + 5);		/* Cylinder High */
	outb(drive << 4, port + 6);	/* Unit */
	outb(WIN_RESTORE, port + 7);	/* Command */

	/* Wait while controller busy. */
	do {
		status = inb(port + 0x206);
		if (status == 0xff) {
			/* No drive present. */
			return 1;
		}
	} while (status & IDE_BUSY_STAT);

	return 0;
}

const char *
get_identify_floppy(unsigned short drive)
{
	static const char * const floppy_type[] = {
		"None",
		"360K, 5.25 in.",
		"1.2M, 5.25 in.",
		"720K, 3.5 in.",
		"1.44M, 3.5 in.",
		"2.88M, 3.5 in.",
		"160K, 5.25 in.",
		"180K, 5.25 in.",
		"320K, 5.25 in.",
	};
	unsigned char drive_type;

	drive_type = cmos_get(fd_config);

	if (drive == 0) {
		drive_type >>= 4;
	} else {
		drive_type &= 0xf;
	}
	
	return floppy_type[drive_type];
}

void
floppy_init(void)
{
	uint8_t drive_type;
	uint16_t equipment;

	/* Reset floppy controller. */
	outb(0x08, 0x3f2);
	outb(0x0c, 0x3f2);

	/* Call sense interrupt status. */
	outb(0x08, 0x3f5);

	/* Get result bytes. */
	(void) inb(0x3f5);
	(void) inb(0x3f5);

	/* Call configure to enable implied seeks. */
	outb(0x13, 0x3f5); /* Configure command */
	outb(0x00, 0x3f5); /* Dummy */
	outb(0 << 7 /* ? */
	   | 1 << 6 /* Enable implied seek */
	   | 1 << 5 /* Enable FIFO */
	   | 1 << 4 /* Disable polling */
	   | 0 << 0, /* FIFO threshold = 1 */
		0x3f5);
	outb(0x00, 0x3f5); /* Precompensation beginning at track 0 */

	/* Get result bytes. */
	/* None. */

	/* int $0x1e vector is diskette_param_table. */
	put_word(0x0000, 0x1e * 4 + 0, PTR_OFF(&diskette_param_table));
	put_word(0x0000, 0x1e * 4 + 2, PTR_SEG(&diskette_param_table));

	/* adjust sys_conf settings for floppy drives */
	/* bit   0: drives are present */
	/* bit 7-6: number of drives - 1 */
	drive_type = cmos_get(fd_config);
	equipment = var_get(sys_conf) & ~0xc1;
	if ((drive_type >> 4) & 0xf) {
		equipment |= (1 << 0);
	}
	if ((drive_type >> 0) & 0xf) {
		equipment |= (1 << 6);
	}
	var_put(sys_conf, equipment);
}

static void
detecting(unsigned short contr, unsigned short unit)
{
	bprintf("  Detecting IDE ");
	if (contr == 0) {
		if (unit == 0) {
			bprintf("Primary Master  ");
		} else {
			bprintf("Primary Slave   ");
		}
	} else if (contr == 1) {
		if (unit == 0) {
			bprintf("Secondary Master");
		} else {
			bprintf("Secondary Slave ");
		}
	} else {
		bprintf("%d/%d             ", contr, unit);
	}
	bprintf("... ");
}


void
disk_init(void)
{
	unsigned char hd;
	unsigned char cd;
	unsigned short contr;
	unsigned short unit;
	unsigned short i;

	hd = 0x80;
	cd = 0xe0;
	ebda_put(harddrives, 0);
	ebda_put(cdcount, 0);
	var_put(hd_num, 0);
	cmos_put(hd_config, 0x00);
	cmos_put(hd_extconfig0, 0);
	cmos_put(hd_extconfig1, 0);
	cmos_put(hd_extconfig2, 0);
	cmos_put(hd_extconfig3, 0);

	for (contr = 0; contr < 6; contr++) {
		for (unit = 0; unit < 2; unit++) {
			const unsigned int nr = contr * 2 + unit;
			unsigned short buffer[512 / sizeof(unsigned short)];
			struct hd_driveid *id = (struct hd_driveid *) buffer;
			int type; /* none == -1; disk == 0; cdrom == 1*/

			if (contr < 2) {
				detecting(contr, unit);
			}

			/* First try disk. */
			if (! get_identify_ide(WIN_IDENTIFY,
					contr, unit, buffer)) {
				/* Harddisk */
				type = 0;
				ebda_put(devices[nr].drive, hd);
				ebda_put(devices[nr].pchs_cylinders, id->cyls);
				ebda_put(devices[nr].pchs_heads, id->heads);
				ebda_put(devices[nr].pchs_spt, id->sectors);
				ebda_put(devices[nr].lchs_cylinders, id->cur_cyls);
				ebda_put(devices[nr].lchs_heads, id->cur_heads);
				ebda_put(devices[nr].lchs_spt, id->cur_sectors);

				ebda_put(devices[nr].sectors, id->lba_capacity);
				ebda_put(devices[nr].type, 0);

				if (hd - 0x80 < 2) {
					ebda_put(hdpt[hd - 0x80].cyls, id->cyls);
					ebda_put(hdpt[hd - 0x80].heads, id->heads);
					ebda_put(hdpt[hd - 0x80].reserved0, 0);
					ebda_put(hdpt[hd - 0x80].precomp, -1);
					ebda_put(hdpt[hd - 0x80].reserved1, 0);
					ebda_put(hdpt[hd - 0x80].control, (8 < id->heads) << 3);
					ebda_put(hdpt[hd - 0x80].reserved2, 0);
					ebda_put(hdpt[hd - 0x80].reserved3, 0);
					ebda_put(hdpt[hd - 0x80].reserved4, 0);
					ebda_put(hdpt[hd - 0x80].landing, id->cyls - 1);
					ebda_put(hdpt[hd - 0x80].secs, id->sectors);
					ebda_put(hdpt[hd - 0x80].reserved5, 0);
				}
				if (hd == 0x80) {
					cmos_put(hd_config,
						0xf0 | cmos_get(hd_config));
					cmos_put(hd_extconfig0, 0x2f);
				} else if (hd == 0x81) {
					cmos_put(hd_config,
						0x0f | cmos_get(hd_config));
					cmos_put(hd_extconfig1, 0x2f);
				} else if (hd == 0x82) {
					cmos_put(hd_extconfig2, 0x2f);
				} else if (hd == 0x83) {
					cmos_put(hd_extconfig2, 0x2f);
				}
				if (nr == 0) {
					/* Primary Master */
					cmos_put(hd_config_user0.cyls,
							id->cyls);
					cmos_put(hd_config_user0.heads,
							id->heads);
					cmos_put(hd_config_user0.precomp,
							-1);
					cmos_put(hd_config_user0.landing,
							id->cyls - 1);
					cmos_put(hd_config_user0.secs,
							id->sectors);
				} else if (nr == 1) {
					/* Primary Slave */
					cmos_put(hd_config_user1.cyls,
							id->cyls);
					cmos_put(hd_config_user1.heads,
							id->heads);
					cmos_put(hd_config_user1.precomp,
							-1);
					cmos_put(hd_config_user1.landing,
							id->cyls - 1);
					cmos_put(hd_config_user1.secs,
							id->sectors);
				} else if (nr == 2) {
					/* Secondary Master */
					cmos_put(hd_config_user2.cyls,
							id->cyls);
					cmos_put(hd_config_user2.heads,
							id->heads);
					cmos_put(hd_config_user2.precomp,
							-1);
					cmos_put(hd_config_user2.landing,
							id->cyls - 1);
					cmos_put(hd_config_user2.secs,
							id->sectors);
				} else if (nr == 3) {
					/* Secondary Slave */
					cmos_put(hd_config_user3.cyls,
							id->cyls);
					cmos_put(hd_config_user3.heads,
							id->heads);
					cmos_put(hd_config_user3.precomp,
							-1);
					cmos_put(hd_config_user3.landing,
							id->cyls - 1);
					cmos_put(hd_config_user3.secs,
							id->sectors);
				}

				ebda_put(hdidmap[hd - 0x80], nr);

				hd++;

				ebda_put(harddrives, ebda_get(harddrives) + 1);
				var_put(hd_num, var_get(hd_num) + 1);

			} else if (! get_identify_ide(WIN_PIDENTIFY,
					contr, unit, buffer)) {
				/* CD-ROM */
				type = 1;
				ebda_put(devices[nr].drive, cd);
				ebda_put(devices[nr].type, 1);

				ebda_put(cdidmap[cd - 0xe0], nr);

				cd++;

				ebda_put(cdcount, ebda_get(cdcount) + 1);

			} else {
				/* nothing */
				ebda_put(devices[nr].drive, 0xff);
				ebda_put(devices[nr].type, 0xfe);
				type = -1;
			}

			if (2 <= contr && type != -1) {
				detecting(contr, unit);
			}

			if (contr < 2 && type == -1) {
				bprintf("None\n");
			} else if (type != -1) {
				/* display device name */
				for (i = 0; i < 40; i++) {
					if (i % 2) {
						putchar(id->model[i-1]);
					} else {
						putchar(id->model[i+1]);
					}
				}
				putchar('\n');
			}
		}
	}

	for ( ; hd < 0x80 + MAX_BIOS_HDS; hd++) {
		ebda_put(hdidmap[hd - 0x80], MAX_BIOS_HDS);
	}
	for ( ; cd < 0xe0 + MAX_BIOS_HDS; cd++) {
		ebda_put(cdidmap[cd - 0xe0], MAX_BIOS_HDS);
	}

	/* int $0x41 vector is harddisk0_param_table. */
	put_word(0x0000, 0x41 * 4 + 0, offsetof(struct ebda, hdpt[0]));
	put_word(0x0000, 0x41 * 4 + 2, EBDA_SEG);

	/* int $0x46 vector is harddisk1_param_table. */
	put_word(0x0000, 0x46 * 4 + 0, offsetof(struct ebda, hdpt[1]));
	put_word(0x0000, 0x46 * 4 + 2, EBDA_SEG);

	/*
	 * WARNING: Minix3 needs HDs accessed last.
	 * So just recalibrate HDs.
	 */
	for (hd = 0x80; hd < 0x80 + MAX_BIOS_HDS; hd++) {
		unsigned int nr;

		nr = ebda_get(hdidmap[hd - 0x80]);

		if (nr != MAX_BIOS_HDS) {
			recalibrate(nr / 2, nr % 2);
		}
	}
}

#endif /* INIT_RM */