[FREELDR] Limit the usage of DiskStopFloppyMotor() in hardware/platform-specific...
[reactos.git] / boot / freeldr / freeldr / arch / i386 / entry.S
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2002 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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <asm.inc>
21 #include <arch/pc/x86common.h>
22 #include <arch/pc/pcbios.h>
23
24 EXTERN _BootMain:PROC
25 EXTERN _InitIdt:PROC
26 EXTERN _i386Idt:DWORD
27 //EXTERN _i386idtptr:FWORD
28 EXTERN cmdline:DWORD
29
30 EXTERN _DiskStopFloppyMotor:PROC
31
32 #ifdef _USE_ML
33 EXTERN __bss_start__:DWORD
34 EXTERN __bss_end__:DWORD
35 #endif
36
37 .code32
38
39 PUBLIC _RealEntryPoint
40 _RealEntryPoint:
41
42 /* Setup segment selectors */
43 mov ax, PMODE_DS
44 mov ds, ax
45 mov es, ax
46 mov fs, ax
47 mov gs, ax
48 mov ss, ax
49
50 /* Setup protected mode stack */
51 mov esp, dword ptr ds:[stack32]
52
53 /* Load the IDT */
54 #ifdef _USE_ML
55 lidt fword ptr ds:[i386idtptr]
56 #else
57 lidt i386idtptr
58 #endif
59
60 /* Continue execution */
61 jmp dword ptr ds:[ContinueAddress]
62
63 PUBLIC ContinueAddress
64 ContinueAddress:
65 .long _FrldrStartup
66
67 _FrldrStartup:
68
69 /* Store BootDrive and BootPartition */
70 mov byte ptr ds:[_FrldrBootDrive], dl
71 xor eax, eax
72 mov al, dh
73 mov dword ptr ds:[_FrldrBootPartition], eax
74
75 /* Patch long jump with real mode entry point */
76 mov eax, dword ptr ds:[BSS_RealModeEntry]
77 mov dword ptr ds:[SwitchToReal16Address], eax
78
79 /* Clean out BSS */
80 xor eax, eax
81 mov edi, offset __bss_start__
82 mov ecx, offset __bss_end__ + 3
83 sub ecx, edi
84 shr ecx, 2
85 rep stosd
86
87 /* Initialize the idt */
88 call _InitIdt
89
90 /* Pass the command line to BootMain */
91 mov eax, offset cmdline
92
93 /* GO! */
94 push eax
95 call _BootMain
96
97 /* We should never get here */
98 stop:
99 jmp short stop
100 nop
101 nop
102
103
104 PUBLIC _Reboot
105 _Reboot:
106 /* Stop the floppy drive motor */
107 call _DiskStopFloppyMotor
108
109 /* Set the function ID */
110 mov bx, FNID_Reboot
111
112 /* Switch to real mode (we don't return) */
113 jmp SwitchToReal
114
115
116 /*
117 * VOID __cdecl ChainLoadBiosBootSectorCode(
118 * IN UCHAR BootDrive OPTIONAL,
119 * IN ULONG BootPartition OPTIONAL);
120 *
121 * RETURNS: Nothing
122 */
123 PUBLIC _ChainLoadBiosBootSectorCode
124 _ChainLoadBiosBootSectorCode:
125 /* Set the boot drive */
126 mov dl, [esp + 4]
127 test dl, dl
128 jnz set_part
129 mov dl, byte ptr ds:[_FrldrBootDrive]
130
131 /* Set the boot partition */
132 set_part:
133 mov eax, [esp + 8]
134 test eax, eax
135 jnz continue
136 mov eax, dword ptr ds:[_FrldrBootPartition]
137 continue:
138 /* Store the 1-byte truncated partition number in DH */
139 mov dh, al
140
141 /*
142 * Don't stop the floppy drive motor when we are just booting a bootsector,
143 * a drive, or a partition. If we were to stop the floppy motor, the BIOS
144 * wouldn't be informed and if the next read is to a floppy then the BIOS
145 * will still think the motor is on and this will result in a read error.
146 */
147 // call _DiskStopFloppyMotor
148
149 /* Set the function ID */
150 mov bx, FNID_ChainLoadBiosBootSectorCode
151
152 /* Switch to real mode (we don't return) */
153 jmp SwitchToReal
154
155
156 /*
157 * U16 PxeCallApi(U16 Segment, U16 Offset, U16 Service, VOID *Parameter);
158 *
159 * RETURNS:
160 */
161 PUBLIC _PxeCallApi
162 _PxeCallApi:
163 /* copy entry point */
164 mov eax, [esp + 4]
165 shl eax, 16
166 mov ax, [esp + 8]
167 mov dword ptr ds:[BSS_PxeEntryPoint], eax
168
169 /* copy function */
170 mov ax, [esp + 12]
171 mov word ptr ds:[BSS_PxeFunction], ax
172
173 /* convert pointer to data buffer to segment/offset */
174 mov eax, [esp + 16]
175 shr eax, 4
176 and eax, HEX(0f000)
177 mov word ptr ds:[BSS_PxeBufferSegment], ax
178 mov eax, [esp + 16]
179 and eax, HEX(0ffff)
180 mov word ptr ds:[BSS_PxeBufferOffset], ax
181
182 pusha
183
184 /* Set the function ID and call realmode */
185 mov bx, FNID_PxeCallApi
186 call i386CallRealMode
187
188 popa
189
190 mov ax, word ptr [BSS_PxeResult]
191
192 ret
193
194
195 PUBLIC i386CallRealMode
196 i386CallRealMode:
197 /* Set continue address and switch to real mode */
198 mov dword ptr ds:[ContinueAddress], offset i386CallRealMode_return
199 jmp SwitchToReal
200 i386CallRealMode_return:
201 ret
202
203
204 /* Entrypoint for realmode function calls
205 * ContinueAddress must be set to the return point from realmode
206 * bx must be set to the ID of the realmode function to call. */
207 PUBLIC SwitchToReal
208 SwitchToReal:
209 /* Set sane segments */
210 mov ax, PMODE_DS
211 mov ds, ax
212 mov es, ax
213 mov fs, ax
214 mov gs, ax
215 mov ss, ax
216
217 /* Save 32-bit stack pointer */
218 mov dword ptr ds:[stack32], esp
219
220 /* jmp to 16-bit segment to set the limit correctly */
221 .byte HEX(0ea) // jmp far RMODE_CS:switch_to_real16
222 SwitchToReal16Address:
223 .long 0 // receives address of switch_to_real16
224 .word RMODE_CS
225 nop
226
227
228 /* 16-bit stack pointer */
229 stack16:
230 .word STACK16ADDR
231
232 /* 32-bit stack pointer */
233 stack32:
234 .long STACKADDR
235
236 .align 4 /* force 4-byte alignment */
237 gdt:
238 /* NULL Descriptor */
239 .word HEX(0000)
240 .word HEX(0000)
241 .word HEX(0000)
242 .word HEX(0000)
243
244 /* 32-bit flat CS */
245 .word HEX(FFFF)
246 .word HEX(0000)
247 .word HEX(9A00)
248 .word HEX(00CF)
249
250 /* 32-bit flat DS */
251 .word HEX(FFFF)
252 .word HEX(0000)
253 .word HEX(9200)
254 .word HEX(00CF)
255
256 /* 16-bit real mode CS */
257 .word HEX(FFFF)
258 .word HEX(0000)
259 .word HEX(9E00)
260 .word HEX(0000)
261
262 /* 16-bit real mode DS */
263 .word HEX(FFFF)
264 .word HEX(0000)
265 .word HEX(9200)
266 .word HEX(0000)
267
268 /* GDT table pointer */
269 gdtptr:
270 .word HEX(27) /* Limit */
271 .long gdt /* Base Address */
272
273 // See _i386IdtDescriptor
274 PUBLIC i386idtptr
275 i386idtptr:
276 .word 255 /* Limit */
277 .long _i386Idt /* Base Address */
278
279 PUBLIC _FrldrBootDrive
280 _FrldrBootDrive:
281 .byte 0
282
283 PUBLIC _FrldrBootPartition
284 _FrldrBootPartition:
285 .long 0
286
287 END