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