[FREELDR]
[reactos.git] / reactos / boot / freeldr / freeldr / arch / realmode / i386.S
1
2 #include <asm.inc>
3 #include "../../include/arch/pc/x86common.h"
4
5 #define IMAGE_DOS_HEADER_e_lfanew 60
6 #define IMAGE_FILE_HEADER_SIZE 20
7 #define IMAGE_OPTIONAL_HEADER_AddressOfEntryPoint 16
8
9 .code16
10
11 /* fat helper code */
12 #include "fathelp.inc"
13
14 .org 512
15 RealModeEntryPoint:
16
17 cli
18
19 /* Setup segment registers */
20 xor ax, ax
21 mov ds, ax
22 mov es, ax
23 mov fs, ax
24 mov gs, ax
25 mov ss, ax
26
27 /* Setup the stack */
28 mov sp, word ptr ds:stack16
29
30 /* Enable A20 address line */
31 call EnableA20
32
33 /* Safe real mode entry point in shared memory */
34 mov dword ptr [BSS_RealModeEntry], offset switch_to_real16
35
36 /* Get address of optional header */
37 mov eax, dword ptr ds:[FREELDR_PE_BASE + IMAGE_DOS_HEADER_e_lfanew]
38 add eax, FREELDR_PE_BASE + 4 + IMAGE_FILE_HEADER_SIZE
39
40 /* Get address of entry point */
41 mov eax, dword ptr ds:[eax + IMAGE_OPTIONAL_HEADER_AddressOfEntryPoint]
42 add eax, FREELDR_PE_BASE
43
44 /* Patch the long jump instruction */
45 mov word ptr [pm_offset], ax
46
47 jmp exit_to_protected
48
49
50 /* This is the entry point from protected mode */
51 switch_to_real16:
52
53 /* Restore segment registers to correct limit */
54 mov ax, RMODE_DS
55 mov ds, ax
56 mov es, ax
57 mov fs, ax
58 mov gs, ax
59 mov ss, ax
60
61 /* Disable Protected Mode */
62 mov eax, cr0
63 and eax, CR0_PE_CLR
64 mov cr0, eax
65
66 /* Clear prefetch queue & correct CS */
67 ljmp16 0, inrmode
68
69 inrmode:
70 /* Set real mode segments */
71 xor ax, ax
72 mov ds, ax
73 mov es, ax
74 mov fs, ax
75 mov gs, ax
76 mov ss, ax
77
78 /* Clear out the high 16-bits of ESP */
79 /* This is needed because I have one */
80 /* machine that hangs when booted to dos if */
81 /* anything other than 0x0000 is in the high */
82 /* 16-bits of ESP. Even though real-mode */
83 /* code should only use SP and not ESP. */
84 xor esp, esp
85
86 /* Restore real mode stack */
87 mov sp, word ptr ds:[stack16]
88
89 /* Load IDTR with real mode value */
90 lidt rmode_idtptr
91
92 sti /* These are ok now */
93
94 /* Do the callback, specified by bx */
95 shl bx, 1
96 call word ptr ds:[callback_table + bx]
97
98
99 /*
100 * Switches the processor to protected mode
101 * it destroys eax
102 */
103 exit_to_protected:
104
105 cli
106
107 /* Safe current stack pointer */
108 mov word ptr ds:[stack16], sp
109
110 /* Load the GDT */
111 lgdt gdtptr
112
113 /* Enable Protected Mode */
114 mov eax, cr0
115 or eax, CR0_PE_SET
116 mov cr0, eax
117
118 /* Clear prefetch queue & correct CS */
119 .byte HEX(0ea) // jmp far PMODE_CS:entry_point
120 pm_offset:
121 .word 0 // receives address of PE entry point
122 .word PMODE_CS
123 nop
124 // FIXME: use ljmp16 PMODE_CS:inpmode + hexed 32bit jump
125
126
127
128 callback_table:
129 .word Int386
130 .word SoftReboot
131 .word ChainLoadBiosBootSectorCode
132 .word PxeCallApi
133 .word PnpBiosGetDeviceNodeCount
134 .word PnpBiosGetDeviceNode
135
136
137 /* 16-bit stack pointer */
138 stack16:
139 .word STACK16ADDR
140
141
142 .align 4 /* force 4-byte alignment */
143 gdt:
144 /* NULL Descriptor */
145 .word HEX(0000)
146 .word HEX(0000)
147 .word HEX(0000)
148 .word HEX(0000)
149
150 /* 32-bit flat CS */
151 .word HEX(FFFF)
152 .word HEX(0000)
153 .word HEX(9A00)
154 .word HEX(00CF)
155
156 /* 32-bit flat DS */
157 .word HEX(FFFF)
158 .word HEX(0000)
159 .word HEX(9200)
160 .word HEX(00CF)
161
162 /* 16-bit real mode CS */
163 .word HEX(FFFF)
164 .word HEX(0000)
165 .word HEX(9E00)
166 .word HEX(0000)
167
168 /* 16-bit real mode DS */
169 .word HEX(FFFF)
170 .word HEX(0000)
171 .word HEX(9200)
172 .word HEX(0000)
173
174 /* GDT table pointer */
175 gdtptr:
176 .word HEX(27) /* Limit */
177 .long gdt /* Base Address */
178
179 /* Real-mode IDT pointer */
180 rmode_idtptr:
181 .word HEX(3ff) /* Limit */
182 .long 0 /* Base Address */
183
184 //.org 1024
185
186 #include "int386.inc"
187 #include "pxe.inc"
188 #include "pnp.inc"
189 #include "helpers.inc"
190
191 .org (FREELDR_PE_BASE - FREELDR_BASE)
192 .endcode16
193
194 END