@@ -28,24 +28,29 @@ Startup:
2828 mov byte ptr ds : [ BSS_BootDrive ], dl
2929 mov byte ptr ds : [ BSS_BootPartition ], dh
3030
31- / * Setup a real mode stack * /
32- mov sp , word ptr ds : [ stack16 ]
33-
34- / * Output first status * /
35- mov si , offset Msg_Starting
36- call writestr
31+ / * Setup the stack * /
32+ mov sp , STACK16ADDR
3733
3834 / * Enable A20 address line * /
3935 call EnableA20
4036
37+ / * Relocate FreeLdr if necessary * /
38+ call RelocateFreeLdr
39+ ljmp16 FREELDR_BASE / 16 , .SegmentRelocationPoint
40+ .SegmentRelocationPoint:
41+
42+ / * Output first status * /
43+ mov si , offset Msg_Starting
44+ call cs_writestr
45+
4146 / * Check the CPU * /
4247 call CheckFor64BitSupport
4348 test al , al
4449 jnz .LongModeSupported
4550
4651 / * Output failure message * /
4752 mov si , offset Msg_Unsupported
48- call writestr
53+ call cs_writestr
4954
5055 / * Wait for a keypress * /
5156 int HEX( 16 )
@@ -64,19 +69,17 @@ Msg_LongModeSupported:
6469.LongModeSupported:
6570 / * Output status * /
6671 mov si , offset Msg_LongModeSupported
67- call writestr
68-
69- / * Load the GDT * /
70- lgdt lXdtPrefix ds : [ gdtptr ]
72+ call cs_writestr
7173
7274 / * Build the startup page tables * /
7375 call BuildPageTables
7476
7577 / * Store real mode entry point in shared memory * /
76- mov dword ptr ds : [ BSS_RealModeEntry ], offset RealModeEntryPoint
78+ mov dword ptr ds : [ BSS_RealModeEntry ], offset RealModeEntryPoint + FREELDR_BASE
7779
7880 / * Address the image with es segment * /
79- mov ax , FREELDR_PE_BASE / 16
81+ mov ax , cs
82+ add ax , (FREELDR_PE_BASE - FREELDR_BASE) / 16
8083 mov es , ax
8184
8285 / * Get address of optional header * /
@@ -88,15 +91,15 @@ Msg_LongModeSupported:
8891 add eax , FREELDR_PE_BASE
8992
9093 / * Save entry point * /
91- mov dword ptr ds : [ LongModeEntryPoint ], eax
94+ mov dword ptr cs : [ LongModeEntryPoint ], eax
9295
9396 / * Restore es * /
9497 xor ax , ax
9598 mov es , ax
9699
97100 / * Output status * /
98101 mov si , offset Msg_SwitchToLongMode
99- call writestr
102+ call cs_writestr
100103
101104 jmp ExitToLongMode
102105
@@ -109,14 +112,15 @@ gdt:
109112 . word HEX( 0000 ) , HEX( 0000 ) , HEX( 0000 ) , HEX( 0000 ) / * 08 : * /
110113 . word HEX( 0000 ) , HEX( 0000 ) , HEX( 9800 ) , HEX( 0020 ) / * 10 : long mode CS * /
111114 . 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 * /
115+ . word HEX(FFFF) , HEX( 0000 ) , HEX(9B00 ) , HEX(008F ) / * 20 : 16 - bit fl at CS (!) * /
113116 . word HEX(FFFF) , HEX( 0000 ) , HEX( 9200 ) , HEX( 0000 ) / * 28 : 16 - bit real mode DS * /
114117 . word HEX(FFFF) , HEX( 0000 ) , HEX(9B00) , HEX(00CF) / * 30 : comp at mode CS * /
118+ gdt_end:
115119
116120/ * GDT table pointer * /
117121gdtptr:
118- . word HEX( 37 ) / * Limit * /
119- .long OFF(gdt) / * Base Address * /
122+ . word HEX( 37 ) / * Limit * /
123+ .long OFF(gdt) + FREELDR_BASE /* Base Address * /
120124
121125
122126CheckFor64BitSupport:
@@ -134,7 +138,7 @@ CheckFor64BitSupport:
134138 jnz .CheckForPAE
135139
136140 mov si , offset .Msg_NoCpuidSupport
137- call writestr
141+ call cs_writestr
138142 popad
139143 xor al , al
140144 ret
@@ -151,7 +155,7 @@ CheckFor64BitSupport:
151155 je .CheckForLongMode
152156
153157 mov si , offset .Msg_NoPAE
154- call writestr
158+ call cs_writestr
155159 popad
156160 xor al , al
157161 ret
@@ -175,7 +179,7 @@ CheckFor64BitSupport:
175179
176180.NoLongMode:
177181 mov si , offset .Msg_NoLongMode
178- call writestr
182+ call cs_writestr
179183 popad
180184 xor al , al
181185 ret
@@ -265,7 +269,7 @@ RealModeEntryPoint:
265269 mov cr0 , eax
266270
267271 / * Clear prefetch queue & correct CS * /
268- ljmp16 0 , InRealMode
272+ ljmp16 FREELDR_BASE / 16 , InRealMode
269273
270274InRealMode:
271275
@@ -290,28 +294,31 @@ InRealMode:
290294 xor esp , esp
291295
292296 / * Restore real mode stack * /
293- mov sp , word ptr ds : [ stack16 ]
297+ mov sp , word ptr cs : [ stack16 ]
294298
295299 // sti / * These are ok now * /
296300
297301 / * Do the callback , specified by bx * /
298302 shl bx , 1
299- call word ptr ds :CallbackTable [ bx ]
303+ call word ptr cs :CallbackTable [ bx ]
300304
301305ExitToLongMode:
302306 / * Disable interrupts * /
303307 cli
304308
305309 / * Set correct segment registers * /
306- xor ax , ax
307- mov ds , ax
308- mov es , ax
309- mov fs , ax
310- mov gs , ax
311- mov ss , ax
310+ xor ax , ax
311+ mov ds , ax
312+ mov es , ax
313+ mov fs , ax
314+ mov gs , ax
315+ mov ss , ax
312316
313317 / * Save current stack pointer * /
314- mov word ptr ds : [ stack16 ], sp
318+ mov word ptr cs : [ stack16 ], sp
319+
320+ / * Load the GDT * /
321+ lgdt lXdtPrefix cs : [ gdtptr ]
315322
316323 / * Set PAE and PGE: 10100000b * /
317324 mov eax , cr4
@@ -335,7 +342,7 @@ ExitToLongMode:
335342 mov cr0 , eax
336343
337344 / * Clear prefetch queue & correct CS * /
338- ljmp16 LMODE_CS , InLongMode
345+ ljmp16 LMODE_CS , InLongMode + FREELDR_BASE
339346InLongMode:
340347 // DB 66h , 0B8h , 18h , 00h // mov ax , LMODE_DS
341348 // DB 66h , 8Eh , 0D8h // mov ds , ax
@@ -350,6 +357,7 @@ LongModeEntryPoint:
350357
351358 int HEX( 16 )
352359 jmp Reboot
360+ LongModeEntryPointEnd:
353361
354362/ * FNID_ * functions * /
355363CallbackTable:
@@ -363,7 +371,7 @@ CallbackTable:
363371
364372 / * 16 - bit stack pointer * /
365373stack16:
366- . word STACK16ADDR
374+ . word 0
367375
368376
369377#include "int386.inc"
0 commit comments