[FREELDR] Introduce a MSVC "linker script" file that centralizes the commands for...
[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 xor eax, eax
94
95 /* GO! */
96 push eax
97 call _BootMain
98
99 /* We should never get here */
100 stop:
101 jmp short stop
102 nop
103 nop
104
105
106 /*
107 * U16 PxeCallApi(U16 Segment, U16 Offset, U16 Service, VOID *Parameter);
108 *
109 * RETURNS:
110 */
111 PUBLIC _PxeCallApi
112 _PxeCallApi:
113 /* copy entry point */
114 mov eax, [esp + 4]
115 shl eax, 16
116 mov ax, [esp + 8]
117 mov dword ptr ds:[BSS_PxeEntryPoint], eax
118
119 /* copy function */
120 mov ax, [esp + 12]
121 mov word ptr ds:[BSS_PxeFunction], ax
122
123 /* convert pointer to data buffer to segment/offset */
124 mov eax, [esp + 16]
125 shr eax, 4
126 and eax, HEX(0f000)
127 mov word ptr ds:[BSS_PxeBufferSegment], ax
128 mov eax, [esp + 16]
129 and eax, HEX(0ffff)
130 mov word ptr ds:[BSS_PxeBufferOffset], ax
131
132 pusha
133
134 /* Set the function ID and call realmode */
135 mov bx, FNID_PxeCallApi
136 call i386CallRealMode
137
138 popa
139
140 mov ax, word ptr [BSS_PxeResult]
141
142 ret
143
144
145 PUBLIC _Reboot
146 _Reboot:
147 /* Set the function ID */
148 mov bx, FNID_Reboot
149
150 /* Switch to real mode (we don't return) */
151 jmp SwitchToReal
152
153
154 PUBLIC _ChainLoadBiosBootSectorCode
155 _ChainLoadBiosBootSectorCode:
156 /* Set the boot drive */
157 mov dl, byte ptr [_FrldrBootDrive]
158
159 /* Set the function ID */
160 mov bx, FNID_ChainLoadBiosBootSectorCode
161
162 /* Switch to real mode (we don't return) */
163 jmp SwitchToReal
164
165
166 PUBLIC i386CallRealMode
167 i386CallRealMode:
168 /* Set continue address and switch to real mode */
169 mov dword ptr [ContinueAddress], offset i386CallRealMode_return
170 jmp SwitchToReal
171 i386CallRealMode_return:
172 ret
173
174
175 /* Entrypoint for realmode function calls
176 * ContinueAddress must be set to the return point from realmode
177 * bx must be set to the ID of the realmode function to call. */
178 PUBLIC SwitchToReal
179 SwitchToReal:
180 /* Set sane segments */
181 mov ax, PMODE_DS
182 mov ds, ax
183 mov es, ax
184 mov fs, ax
185 mov gs, ax
186 mov ss, ax
187
188 /* Save 32-bit stack pointer */
189 mov dword ptr ds:[stack32], esp
190
191 /* jmp to 16-bit segment to set the limit correctly */
192 .byte HEX(0ea) // jmp far RMODE_CS:switch_to_real16
193 SwitchToReal16Address:
194 .long 0 // receives address of switch_to_real16
195 .word RMODE_CS
196 nop
197
198
199 /* 16-bit stack pointer */
200 stack16:
201 .word STACK16ADDR
202
203 /* 32-bit stack pointer */
204 stack32:
205 .long STACKADDR
206
207 .align 4 /* force 4-byte alignment */
208 gdt:
209 /* NULL Descriptor */
210 .word HEX(0000)
211 .word HEX(0000)
212 .word HEX(0000)
213 .word HEX(0000)
214
215 /* 32-bit flat CS */
216 .word HEX(FFFF)
217 .word HEX(0000)
218 .word HEX(9A00)
219 .word HEX(00CF)
220
221 /* 32-bit flat DS */
222 .word HEX(FFFF)
223 .word HEX(0000)
224 .word HEX(9200)
225 .word HEX(00CF)
226
227 /* 16-bit real mode CS */
228 .word HEX(FFFF)
229 .word HEX(0000)
230 .word HEX(9E00)
231 .word HEX(0000)
232
233 /* 16-bit real mode DS */
234 .word HEX(FFFF)
235 .word HEX(0000)
236 .word HEX(9200)
237 .word HEX(0000)
238
239 /* GDT table pointer */
240 gdtptr:
241 .word HEX(27) /* Limit */
242 .long gdt /* Base Address */
243
244 // See _i386IdtDescriptor
245 PUBLIC i386idtptr
246 i386idtptr:
247 .word 255 /* Limit */
248 .long _i386Idt /* Base Address */
249
250 PUBLIC _FrldrBootDrive
251 _FrldrBootDrive:
252 .byte 0
253
254 PUBLIC _FrldrBootPartition
255 _FrldrBootPartition:
256 .long 0
257
258 END