3 #include "../../include/arch/pc/x86common.h"
5 #define IMAGE_DOS_HEADER_e_lfanew 60
6 #define IMAGE_FILE_HEADER_SIZE 20
7 #define IMAGE_OPTIONAL_HEADER_AddressOfEntryPoint 16
12 #include "fathelp.inc"
19 /* Setup real mode segment registers */
27 /* Save the boot drive and partition */
28 mov byte ptr ds:[BSS_BootDrive], dl
29 mov byte ptr ds:[BSS_BootPartition], dh
31 /* Setup a real mode stack */
32 mov sp, word ptr ds:[stack16]
34 /* Output first status */
35 mov si, offset Msg_Starting
38 /* Enable A20 address line */
42 call CheckFor64BitSupport
44 jnz .LongModeSupported
46 /* Output failure message */
47 mov si, offset Msg_Unsupported
50 /* Wait for a keypress */
55 .ascii "This CPU is not supported.", CR, LF
56 .ascii "Press any key to reboot...", NUL
59 .ascii "Starting FreeLoader...", CR, LF, NUL
61 Msg_LongModeSupported:
62 .ascii "Long mode support detected.", CR, LF, NUL
66 mov si, offset Msg_LongModeSupported
70 lgdt fword ptr [gdtptr]
72 /* Build the startup page tables */
75 /* Store real mode entry point in shared memory */
76 mov dword ptr ds:[BSS_RealModeEntry], offset RealModeEntryPoint
78 /* Address the image with es segment */
79 mov ax, FREELDR_PE_BASE / 16
82 /* Get address of optional header */
83 mov eax, dword ptr es:[IMAGE_DOS_HEADER_e_lfanew]
84 add eax, 4 + IMAGE_FILE_HEADER_SIZE
86 /* Get address of entry point */
87 mov eax, dword ptr es:[eax + IMAGE_OPTIONAL_HEADER_AddressOfEntryPoint]
88 add eax, FREELDR_PE_BASE
90 /* Save entry point */
91 mov dword ptr ds:[LongModeEntryPoint], eax
98 mov si, offset Msg_SwitchToLongMode
103 Msg_SwitchToLongMode:
104 .ascii "Switching to long mode....", CR, LF, NUL
108 .word HEX(0000), HEX(0000), HEX(0000), HEX(0000) /* 00: NULL descriptor */
109 .word HEX(0000), HEX(0000), HEX(0000), HEX(0000) /* 08: */
110 .word HEX(0000), HEX(0000), HEX(9800), HEX(0020) /* 10: long mode cs */
111 .word HEX(ffff), HEX(0000), HEX(f300), HEX(00cf) /* 18: long mode ds */
112 .word HEX(FFFF), HEX(0000), HEX(9E00), HEX(0000) /* 20: 16-bit real mode CS */
113 .word HEX(FFFF), HEX(0000), HEX(9200), HEX(0000) /* 28: 16-bit real mode DS */
114 .word HEX(FFFF), HEX(0000), HEX(9B00), HEX(00CF) /* 30: compat mode cs */
116 /* GDT table pointer */
118 .word HEX(37) /* Limit */
119 .long offset gdt /* Base Address */
122 CheckFor64BitSupport:
123 /* Check if CPU supports CPUID */
128 xor eax, HEX(00200000)
136 mov si, offset .Msg_NoCpuidSupport
143 .ascii "The system doesn't support CPUID.", CR, LF, NUL
146 /* CPUID support detected - getting the PAE/PGE */
147 mov eax,1 // Fn0000_0001 - PAE in EDX[6]
153 mov si, offset .Msg_NoPAE
160 .ascii "PAE or PGE not set.", CR, LF, NUL
164 mov eax, HEX(80000001)
166 and edx, HEX(20000000)
170 mov si, offset .Msg_NoLongMode
177 .ascii "Long mode is not supported.", CR, LF, NUL
190 /* Get segment of the PML4 */
191 mov eax, PML4_ADDRESS / 16
196 /* One entry in the PML4 pointing to PDP */
206 /* One entry in the PDP pointing to PD */
216 /* 512 entries in the PD, each defining a 2MB page each */
222 mov dword ptr es: [di + 4], 0
223 add eax, 512 * 4096 // add 512 4k pages
236 /******************************************************************************/
238 #define MSR_EFER HEX(C0000080)
239 #define LMODE_CS HEX(10)
241 /* This is the entry point from long mode */
243 /* Disable Protected Mode */
245 and eax, HEX(0fffffffe) // ~0x00000001
248 /* Clear prefetch queue & correct CS */
249 ljmp16 0, offset InRealMode
253 // mov ax, HEX(0b800)
255 // mov word ptr es:[12], HEX(0e00) + '6'
257 /* Set real mode segments */
265 /* Clear out the high 16-bits of ESP */
266 /* This is needed because I have one */
267 /* machine that hangs when booted to dos if */
268 /* anything other than 0x0000 is in the high */
269 /* 16-bits of ESP. Even though real-mode */
270 /* code should only use SP and not ESP. */
273 /* Restore real mode stack */
274 mov sp, word ptr ds:[stack16]
276 // sti /* These are ok now */
278 /* Do the callback, specified by bx */
280 call word ptr ds:CallbackTable[bx]
283 /* Disable interrupts */
286 /* Set correct segment registers */
294 /* Save current stack pointer */
295 mov word ptr ds:[stack16], sp
297 /* Set PAE and PGE: 10100000b */
301 /* Point cr3 at the PML4 */
302 mov eax, PML4_ADDRESS
305 /* Enable long mode */
308 or eax, HEX(00000100)
311 /* Activate long mode by enabling paging and protection simultaneously,
312 skipping protected mode entirely */
314 or eax, HEX(80000001)
317 /* Clear prefetch queue & correct CS */
318 ljmp16 LMODE_CS, InLongMode
320 //DB 66h, 0B8h, 18h, 00h // mov ax, LMODE_DS
321 //DB 66h, 8Eh, 0D8h // mov ds, ax
322 //DB 66h, 66h, 0C7h, 04h, 25h, 00h, 80h, 0Bh, 00h, 31h, 0Eh
323 //mov word ptr [HEX(b8000)], HEX(0e00) + '1'
325 .byte HEX(0ff), HEX(25) // opcode of 64bit indirect jump
326 .long 1 // relative address of LongModeEntryPoint
337 .word ChainLoadBiosBootSectorCode
339 .word PnpBiosGetDeviceNodeCount
340 .word PnpBiosGetDeviceNode
341 .word 0 // BootLinuxKernel
343 /* 16-bit stack pointer */
348 #include "int386.inc"
351 #include "helpers.inc"
353 .org (FREELDR_PE_BASE - FREELDR_BASE - 1)