[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 /* Get address of optional header */
34 mov eax, dword ptr ds:[FREELDR_PE_BASE + IMAGE_DOS_HEADER_e_lfanew]
35 add eax, FREELDR_PE_BASE + 4 + IMAGE_FILE_HEADER_SIZE
36
37 /* Get address of entry point */
38 mov eax, dword ptr ds:[eax + IMAGE_OPTIONAL_HEADER_AddressOfEntryPoint]
39 add eax, FREELDR_PE_BASE
40
41 /* Safe the entry point */
42 mov dword ptr [BSS_EntryPoint], eax
43
44 /* Patch the long jump instruction */
45 mov word ptr [pm_offset], ax
46
47 /*
48 * Switches the processor to protected mode
49 * it destroys eax
50 */
51 switch_to_prot:
52
53 /* Load the GDT */
54 lgdt gdtptr
55
56 /* Enable Protected Mode */
57 mov eax, cr0
58 or eax, CR0_PE_SET
59 mov cr0, eax
60
61 /* Clear prefetch queue & correct CS */
62 .byte HEX(0ea) // jmp far PMODE_CS:entry_point
63 pm_offset:
64 .word 0 // receives address of PE entry point
65 .word PMODE_CS
66 nop
67
68
69
70 /* 16-bit stack pointer */
71 stack16:
72 .word STACK16ADDR
73
74
75 .align 4 /* force 4-byte alignment */
76 gdt:
77 /* NULL Descriptor */
78 .word HEX(0000)
79 .word HEX(0000)
80 .word HEX(0000)
81 .word HEX(0000)
82
83 /* 32-bit flat CS */
84 .word HEX(FFFF)
85 .word HEX(0000)
86 .word HEX(9A00)
87 .word HEX(00CF)
88
89 /* 32-bit flat DS */
90 .word HEX(FFFF)
91 .word HEX(0000)
92 .word HEX(9200)
93 .word HEX(00CF)
94
95 /* 16-bit real mode CS */
96 .word HEX(FFFF)
97 .word HEX(0000)
98 .word HEX(9E00)
99 .word HEX(0000)
100
101 /* 16-bit real mode DS */
102 .word HEX(FFFF)
103 .word HEX(0000)
104 .word HEX(9200)
105 .word HEX(0000)
106
107 /* GDT table pointer */
108 gdtptr:
109 .word HEX(27) /* Limit */
110 .long gdt /* Base Address */
111
112 .org 1024
113
114 #include "helpers.inc"
115
116 .org (FREELDR_PE_BASE - FREELDR_BASE)
117 .endcode16
118
119 END