Sync with trunk r63793.
[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
29 .code32
30
31 PUBLIC _RealEntryPoint
32 _RealEntryPoint:
33
34 /* Setup segment selectors */
35 mov ax, PMODE_DS
36 mov ds, ax
37 mov es, ax
38 mov fs, ax
39 mov gs, ax
40 mov ss, ax
41
42 /* Setup protected mode stack */
43 mov esp, dword ptr ds:[stack32]
44
45 /* Load the IDT */
46 #ifdef _USE_ML
47 lidt fword ptr ds:[i386idtptr]
48 #else
49 lidt i386idtptr
50 #endif
51
52 /* Continue execution */
53 jmp dword ptr ds:[ContinueAddress]
54
55 ContinueAddress:
56 .long _FrldrStartup
57
58
59 _FrldrStartup:
60
61 /* Store BootDrive and BootPartition */
62 mov byte ptr ds:[_FrldrBootDrive], dl
63 xor eax, eax
64 mov al, dh
65 mov dword ptr ds:[_FrldrBootPartition], eax
66
67 /* Patch long jump with real mode entry point */
68 mov eax, dword ptr ds:[BSS_RealModeEntry]
69 mov dword ptr ds:[SwitchToReal16Address], eax
70
71 /* Initialize the idt */
72 call _InitIdt
73
74 #ifndef _USE_ML
75 /* Clean out bss */
76 xor eax, eax
77 mov edi, offset __bss_start__
78 mov ecx, offset __bss_end__ + 3
79 sub ecx, edi
80 shr ecx, 2
81 rep stosd
82
83 /* Pass the command line to BootMain */
84 mov eax, offset cmdline
85 #else
86 xor eax, eax
87 #endif
88
89 /* GO! */
90 push eax
91 call _BootMain
92
93 /* We should never get here */
94 stop:
95 jmp stop
96 nop
97 nop
98
99 Int386_regsin:
100 .long 0
101 Int386_regsout:
102 .long 0
103
104 /*
105 * int Int386(int ivec, REGS* in, REGS* out);
106 */
107 PUBLIC _Int386
108 _Int386:
109
110 /* Get the function parameters */
111 mov eax, dword ptr [esp + 4]
112 mov dword ptr ds:[BSS_IntVector], eax
113 mov eax, dword ptr [esp + 8]
114 mov dword ptr [Int386_regsin], eax
115 mov eax, dword ptr [esp + 12]
116 mov dword ptr [Int386_regsout], eax
117
118 /* Save all registers + segment registers */
119 push ds
120 push es
121 push fs
122 push gs
123 pusha
124
125 /* Copy input registers */
126 mov esi, dword ptr [Int386_regsin]
127 mov edi, BSS_RegisterSet
128 mov ecx, REGS_SIZE / 4
129 rep movsd
130
131 /* Set the function ID */
132 mov bx, FNID_Int386
133
134 /* Set continue address and switch to real mode */
135 mov dword ptr [ContinueAddress], offset Int386_return
136 jmp SwitchToReal
137
138 Int386_return:
139
140 /* Copy output registers */
141 mov esi, BSS_RegisterSet
142 mov edi, dword ptr [Int386_regsout]
143 mov ecx, REGS_SIZE / 4
144 rep movsd
145
146 popa
147 pop gs
148 pop fs
149 pop es
150 pop ds
151 ret
152
153
154 /*
155 * U16 PxeCallApi(U16 Segment, U16 Offset, U16 Service, VOID *Parameter);
156 *
157 * RETURNS:
158 */
159 PUBLIC _PxeCallApi
160 _PxeCallApi:
161 push ebp
162 mov ebp, esp
163
164 pusha
165 push es
166
167 /* copy entry point */
168 mov eax, [ebp + 8]
169 shl eax, 16
170 mov ax, [ebp + 12]
171 mov dword ptr ds:[BSS_PxeEntryPoint], eax
172
173 /* copy function */
174 mov ax, [ebp + 16]
175 mov word ptr ds:[BSS_PxeFunction], ax
176
177 /* convert pointer to data buffer to segment/offset */
178 mov eax, [ebp + 20]
179 shr eax, 4
180 and eax, HEX(0f000)
181 mov word ptr ds:[BSS_PxeBufferSegment], ax
182 mov eax, [ebp + 20]
183 and eax, HEX(0ffff)
184 mov word ptr ds:[BSS_PxeBufferOffset], ax
185
186 /* Set the function ID and call realmode */
187 mov bx, FNID_PxeCallApi
188 call i386CallRealMode
189
190 pop es
191 popa
192
193 mov esp, ebp
194 pop ebp
195
196 mov ax, word ptr [BSS_PxeResult]
197
198 ret
199
200
201 PUBLIC _Reboot
202 _Reboot:
203 /* Set the function ID */
204 mov bx, FNID_Reboot
205
206 /*Switch to real mode (We don't return) */
207 jmp SwitchToReal
208
209
210 PUBLIC _ChainLoadBiosBootSectorCode
211 _ChainLoadBiosBootSectorCode:
212 /* Set the boot drive */
213 mov dl, byte ptr [_FrldrBootDrive]
214
215 /* Set the function ID */
216 mov bx, FNID_ChainLoadBiosBootSectorCode
217
218 /*Switch to real mode (We don't return) */
219 jmp SwitchToReal
220
221
222 PUBLIC i386CallRealMode
223 i386CallRealMode:
224 /* Set continue address and switch to real mode */
225 mov dword ptr [ContinueAddress], offset i386CallRealMode_return
226 jmp SwitchToReal
227 i386CallRealMode_return:
228 ret
229
230
231 /* Entrypoint for realmode function calls
232 * ContinueAddress must be set to the return point from realmode
233 * bx must be set to the ID of the realmode function to call. */
234 SwitchToReal:
235 /* Set sane segments */
236 mov ax, PMODE_DS
237 mov ds, ax
238 mov es, ax
239 mov fs, ax
240 mov gs, ax
241 mov ss, ax
242
243 /* Save 32-bit stack pointer */
244 mov dword ptr [stack32], esp
245
246 /* jmp to 16-bit segment to set the limit correctly */
247 .byte HEX(0ea) // jmp far RMODE_CS:switch_to_real16
248 SwitchToReal16Address:
249 .long 0 // receives address of switch_to_real16
250 .word RMODE_CS
251 nop
252
253
254 /* 16-bit stack pointer */
255 stack16:
256 .word STACK16ADDR
257
258 /* 32-bit stack pointer */
259 stack32:
260 .long STACK32ADDR
261
262 .align 4 /* force 4-byte alignment */
263 gdt:
264 /* NULL Descriptor */
265 .word HEX(0000)
266 .word HEX(0000)
267 .word HEX(0000)
268 .word HEX(0000)
269
270 /* 32-bit flat CS */
271 .word HEX(FFFF)
272 .word HEX(0000)
273 .word HEX(9A00)
274 .word HEX(00CF)
275
276 /* 32-bit flat DS */
277 .word HEX(FFFF)
278 .word HEX(0000)
279 .word HEX(9200)
280 .word HEX(00CF)
281
282 /* 16-bit real mode CS */
283 .word HEX(FFFF)
284 .word HEX(0000)
285 .word HEX(9E00)
286 .word HEX(0000)
287
288 /* 16-bit real mode DS */
289 .word HEX(FFFF)
290 .word HEX(0000)
291 .word HEX(9200)
292 .word HEX(0000)
293
294 /* GDT table pointer */
295 gdtptr:
296 .word HEX(27) /* Limit */
297 .long gdt /* Base Address */
298
299 /* Real-mode IDT pointer */
300 rmode_idtptr:
301 .word HEX(3ff) /* Limit */
302 .long 0 /* Base Address */
303
304 PUBLIC i386idtptr
305 i386idtptr:
306 .word 255 /* Limit */
307 .long _i386Idt /* Base Address */
308
309 PUBLIC _FrldrBootDrive
310 _FrldrBootDrive:
311 .byte 0
312
313 PUBLIC _FrldrBootPartition
314 _FrldrBootPartition:
315 .long 0
316
317 END