7de40ca0263a2a9084d376cbc7924cd3ed31dfd8
[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 #ifdef _USE_ML
31 EXTERN __bss_start__:DWORD
32 EXTERN __bss_end__:DWORD
33 #endif
34
35 .code32
36
37 PUBLIC _RealEntryPoint
38 _RealEntryPoint:
39
40 /* Setup segment selectors */
41 mov ax, PMODE_DS
42 mov ds, ax
43 mov es, ax
44 mov fs, ax
45 mov gs, ax
46 mov ss, ax
47
48 /* Setup protected mode stack */
49 mov esp, dword ptr ds:[stack32]
50
51 /* Load the IDT */
52 #ifdef _USE_ML
53 lidt fword ptr ds:[i386idtptr]
54 #else
55 lidt i386idtptr
56 #endif
57
58 /* Continue execution */
59 jmp dword ptr ds:[ContinueAddress]
60
61 PUBLIC ContinueAddress
62 ContinueAddress:
63 .long _FrldrStartup
64
65
66 _FrldrStartup:
67
68 ASSUME /*CS:_TEXT,*/ DS:_DATA, ES:_DATA, FS:_DATA, GS:_DATA, SS:_DATA
69
70 /* Store BootDrive and BootPartition */
71 mov byte ptr ds:[_FrldrBootDrive], dl
72 xor eax, eax
73 mov al, dh
74 mov dword ptr ds:[_FrldrBootPartition], eax
75
76 /* Patch long jump with real mode entry point */
77 mov eax, dword ptr ds:[BSS_RealModeEntry]
78 mov dword ptr ds:[SwitchToReal16Address], eax
79
80 /* Clean out BSS */
81 xor eax, eax
82 mov edi, offset __bss_start__
83 mov ecx, offset __bss_end__ + 3
84 sub ecx, edi
85 shr ecx, 2
86 rep stosd
87
88 /* Initialize the idt */
89 call _InitIdt
90
91 /* Pass the command line to BootMain */
92 mov eax, offset cmdline
93
94 /* GO! */
95 push eax
96 call _BootMain
97
98 /* We should never get here */
99 stop:
100 jmp short stop
101 nop
102 nop
103
104
105 /*
106 * U16 PxeCallApi(U16 Segment, U16 Offset, U16 Service, VOID *Parameter);
107 *
108 * RETURNS:
109 */
110 PUBLIC _PxeCallApi
111 _PxeCallApi:
112 /* copy entry point */
113 mov eax, [esp + 4]
114 shl eax, 16
115 mov ax, [esp + 8]
116 mov dword ptr ds:[BSS_PxeEntryPoint], eax
117
118 /* copy function */
119 mov ax, [esp + 12]
120 mov word ptr ds:[BSS_PxeFunction], ax
121
122 /* convert pointer to data buffer to segment/offset */
123 mov eax, [esp + 16]
124 shr eax, 4
125 and eax, HEX(0f000)
126 mov word ptr ds:[BSS_PxeBufferSegment], ax
127 mov eax, [esp + 16]
128 and eax, HEX(0ffff)
129 mov word ptr ds:[BSS_PxeBufferOffset], ax
130
131 pusha
132
133 /* Set the function ID and call realmode */
134 mov bx, FNID_PxeCallApi
135 call i386CallRealMode
136
137 popa
138
139 mov ax, word ptr [BSS_PxeResult]
140
141 ret
142
143
144 PUBLIC _Reboot
145 _Reboot:
146 /* Set the function ID */
147 mov bx, FNID_Reboot
148
149 /* Switch to real mode (we don't return) */
150 jmp SwitchToReal
151
152
153 PUBLIC _ChainLoadBiosBootSectorCode
154 _ChainLoadBiosBootSectorCode:
155 /* Set the boot drive */
156 mov dl, byte ptr [_FrldrBootDrive]
157
158 /* Set the function ID */
159 mov bx, FNID_ChainLoadBiosBootSectorCode
160
161 /* Switch to real mode (we don't return) */
162 jmp SwitchToReal
163
164
165 PUBLIC i386CallRealMode
166 i386CallRealMode:
167 /* Set continue address and switch to real mode */
168 mov dword ptr [ContinueAddress], offset i386CallRealMode_return
169 jmp SwitchToReal
170 i386CallRealMode_return:
171 ret
172
173
174 /* Entrypoint for realmode function calls
175 * ContinueAddress must be set to the return point from realmode
176 * bx must be set to the ID of the realmode function to call. */
177 PUBLIC SwitchToReal
178 SwitchToReal:
179 /* Set sane segments */
180 mov ax, PMODE_DS
181 mov ds, ax
182 mov es, ax
183 mov fs, ax
184 mov gs, ax
185 mov ss, ax
186
187 /* Save 32-bit stack pointer */
188 mov dword ptr ds:[stack32], esp
189
190 /* jmp to 16-bit segment to set the limit correctly */
191 .byte HEX(0ea) // jmp far RMODE_CS:switch_to_real16
192 SwitchToReal16Address:
193 .long 0 // receives address of switch_to_real16
194 .word RMODE_CS
195 nop
196
197
198 /* 16-bit stack pointer */
199 stack16:
200 .word STACK16ADDR
201
202 /* 32-bit stack pointer */
203 stack32:
204 .long STACKADDR
205
206 .align 4 /* force 4-byte alignment */
207 gdt:
208 /* NULL Descriptor */
209 .word HEX(0000)
210 .word HEX(0000)
211 .word HEX(0000)
212 .word HEX(0000)
213
214 /* 32-bit flat CS */
215 .word HEX(FFFF)
216 .word HEX(0000)
217 .word HEX(9A00)
218 .word HEX(00CF)
219
220 /* 32-bit flat DS */
221 .word HEX(FFFF)
222 .word HEX(0000)
223 .word HEX(9200)
224 .word HEX(00CF)
225
226 /* 16-bit real mode CS */
227 .word HEX(FFFF)
228 .word HEX(0000)
229 .word HEX(9E00)
230 .word HEX(0000)
231
232 /* 16-bit real mode DS */
233 .word HEX(FFFF)
234 .word HEX(0000)
235 .word HEX(9200)
236 .word HEX(0000)
237
238 /* GDT table pointer */
239 gdtptr:
240 .word HEX(27) /* Limit */
241 .long gdt /* Base Address */
242
243 // See _i386IdtDescriptor
244 PUBLIC i386idtptr
245 i386idtptr:
246 .word 255 /* Limit */
247 .long _i386Idt /* Base Address */
248
249 PUBLIC _FrldrBootDrive
250 _FrldrBootDrive:
251 .byte 0
252
253 PUBLIC _FrldrBootPartition
254 _FrldrBootPartition:
255 .long 0
256
257 END