[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
125
126 callback_table:
127 .word Int386
128
129
130 /* 16-bit stack pointer */
131 stack16:
132 .word STACK16ADDR
133
134
135 .align 4 /* force 4-byte alignment */
136 gdt:
137 /* NULL Descriptor */
138 .word HEX(0000)
139 .word HEX(0000)
140 .word HEX(0000)
141 .word HEX(0000)
142
143 /* 32-bit flat CS */
144 .word HEX(FFFF)
145 .word HEX(0000)
146 .word HEX(9A00)
147 .word HEX(00CF)
148
149 /* 32-bit flat DS */
150 .word HEX(FFFF)
151 .word HEX(0000)
152 .word HEX(9200)
153 .word HEX(00CF)
154
155 /* 16-bit real mode CS */
156 .word HEX(FFFF)
157 .word HEX(0000)
158 .word HEX(9E00)
159 .word HEX(0000)
160
161 /* 16-bit real mode DS */
162 .word HEX(FFFF)
163 .word HEX(0000)
164 .word HEX(9200)
165 .word HEX(0000)
166
167 /* GDT table pointer */
168 gdtptr:
169 .word HEX(27) /* Limit */
170 .long gdt /* Base Address */
171
172 /* Real-mode IDT pointer */
173 rmode_idtptr:
174 .word HEX(3ff) /* Limit */
175 .long 0 /* Base Address */
176
177 //.org 1024
178
179 #include "int386.inc"
180 #include "helpers.inc"
181
182 .org (FREELDR_PE_BASE - FREELDR_BASE)
183 .endcode16
184
185 END