Merge 13159:13510 from trunk
[reactos.git] / reactos / boot / freeldr / freeldr / arch / i386 / i386disk.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "freeldr.h"
21 #include "debug.h"
22
23 /////////////////////////////////////////////////////////////////////////////////////////////
24 // FUNCTIONS
25 /////////////////////////////////////////////////////////////////////////////////////////////
26
27 #ifdef __i386__
28
29 BOOL DiskResetController(ULONG DriveNumber)
30 {
31 REGS RegsIn;
32 REGS RegsOut;
33
34 DbgPrint((DPRINT_DISK, "DiskResetController(0x%x) DISK OPERATION FAILED -- RESETTING CONTROLLER\n", DriveNumber));
35
36 // BIOS Int 13h, function 0 - Reset disk system
37 // AH = 00h
38 // DL = drive (if bit 7 is set both hard disks and floppy disks reset)
39 // Return:
40 // AH = status
41 // CF clear if successful
42 // CF set on error
43 RegsIn.b.ah = 0x00;
44 RegsIn.b.dl = DriveNumber;
45
46 // Reset the disk controller
47 Int386(0x13, &RegsIn, &RegsOut);
48
49 return INT386_SUCCESS(RegsOut);
50 }
51
52 BOOL DiskInt13ExtensionsSupported(ULONG DriveNumber)
53 {
54 REGS RegsIn;
55 REGS RegsOut;
56
57 DbgPrint((DPRINT_DISK, "DiskInt13ExtensionsSupported()\n"));
58
59 // IBM/MS INT 13 Extensions - INSTALLATION CHECK
60 // AH = 41h
61 // BX = 55AAh
62 // DL = drive (80h-FFh)
63 // Return:
64 // CF set on error (extensions not supported)
65 // AH = 01h (invalid function)
66 // CF clear if successful
67 // BX = AA55h if installed
68 // AH = major version of extensions
69 // 01h = 1.x
70 // 20h = 2.0 / EDD-1.0
71 // 21h = 2.1 / EDD-1.1
72 // 30h = EDD-3.0
73 // AL = internal use
74 // CX = API subset support bitmap
75 // DH = extension version (v2.0+ ??? -- not present in 1.x)
76 //
77 // Bitfields for IBM/MS INT 13 Extensions API support bitmap
78 // Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported
79 // Bit 1, removable drive controller functions (AH=45h,46h,48h,49h,INT 15/AH=52h) supported
80 // Bit 2, enhanced disk drive (EDD) functions (AH=48h,AH=4Eh) supported
81 // extended drive parameter table is valid
82 // Bits 3-15 reserved
83 RegsIn.b.ah = 0x41;
84 RegsIn.w.bx = 0x55AA;
85 RegsIn.b.dl = DriveNumber;
86
87 // Reset the disk controller
88 Int386(0x13, &RegsIn, &RegsOut);
89
90 if (!INT386_SUCCESS(RegsOut))
91 {
92 // CF set on error (extensions not supported)
93 return FALSE;
94 }
95
96 if (RegsOut.w.bx != 0xAA55)
97 {
98 // BX = AA55h if installed
99 return FALSE;
100 }
101
102 // Note:
103 // The original check is too strict because some BIOSes report that
104 // extended disk access functions are not suported when booting
105 // from a CD (e.g. Phoenix BIOS v6.00PG). Argh!
106 #if 0
107 if (!(RegsOut.w.cx & 0x0001))
108 {
109 // CX = API subset support bitmap
110 // Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported
111 return FALSE;
112 }
113 #endif
114
115 // Use this relaxed check instead
116 if (RegsOut.w.cx == 0x0000)
117 {
118 // CX = API subset support bitmap
119 return FALSE;
120 }
121
122 return TRUE;
123 }
124
125 VOID DiskStopFloppyMotor(VOID)
126 {
127 WRITE_PORT_UCHAR((PUCHAR)0x3F2, 0);
128 }
129
130 BOOL DiskGetExtendedDriveParameters(ULONG DriveNumber, PVOID Buffer, USHORT BufferSize)
131 {
132 REGS RegsIn;
133 REGS RegsOut;
134 PUSHORT Ptr = (PUSHORT)(BIOSCALLBUFFER);
135
136 DbgPrint((DPRINT_DISK, "DiskGetExtendedDriveParameters()\n"));
137
138 // Initialize transfer buffer
139 *Ptr = BufferSize;
140
141 // BIOS Int 13h, function 48h - Get drive parameters
142 // AH = 48h
143 // DL = drive (bit 7 set for hard disk)
144 // DS:SI = result buffer
145 // Return:
146 // CF set on error
147 // AH = status (07h)
148 // CF clear if successful
149 // AH = 00h
150 // DS:SI -> result buffer
151 RegsIn.b.ah = 0x48;
152 RegsIn.b.dl = DriveNumber;
153 RegsIn.x.ds = BIOSCALLBUFSEGMENT; // DS:SI -> result buffer
154 RegsIn.w.si = BIOSCALLBUFOFFSET;
155
156 // Get drive parameters
157 Int386(0x13, &RegsIn, &RegsOut);
158
159 if (!INT386_SUCCESS(RegsOut))
160 {
161 return FALSE;
162 }
163
164 memcpy(Buffer, Ptr, BufferSize);
165
166 return TRUE;
167 }
168
169 #endif // defined __i386__