summaryrefslogtreecommitdiff
path: root/boot2/stage2.asm
blob: d4abbca2c3211463869a96527d41ae19cca03134 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;   THE FOOL-BOOT-LOADER
;    
;	* X sectors of our kernel loaded at KERNEL_OFFSET from floppy
;
;	* memory map made available at MEMMAP_OFFSET 
;	    (check at MEMMEP_SIZE_OFFSET for number of entries)
;
;	* the VESA mode specified by VESA_MODE_SELECT will be set up 
;	    (check at VESA_MODES, and VESA_MODE_INFO 
;	    for additional information)
;
;	* interrupts disabled
;
;	* 32-bit protected mode set up.
;
;	* esp set under MBR
;
;	* A20 gate opened 
;
;	* and finally we will jump into the C world to kernel_main() !
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;we want 16-bit instructions, before we switch to 32-bit protected mode.
[bits 16]

;define origin of boot record in memory: 0x7c00 was bootloader0
;this is where the BIOS per definition will put the first 
;512 bytes of data from the boot device
;wer are one sector after that.
[org 0x7e00]

;;define some constants
;;where we will load our kernel into memory and some 
;;other memory locations
;
MEMMAP_SIZE_OFFSET equ 0x7c00
MEMMAP_OFFSET equ 0x7c01
VESA_MODES equ 0x9300		; do NOT overwrite yourself! be careful!
VESA_MODE_INFO equ 0x9400
VESA_MODE_SELECT equ 0x4114
CHUNKS_TO_LOAD equ 0x0a ;number of 0x8000 * 512 byte chunks to load into ram 
;

jmp boot_16 ;start boot process

;;SOME Global Data, mainly info/error strings
FILL:
   times 32  db 0x66

BOOT_DRIVE:
    db 0xff

LOADER_TARGET:
    dd 0x100000  ;here we will put our image

KERNEL_CHUNK:
    dw 0x1
STR_VERSION:
    db "Fool Loader Stage 2 v0.5",0

STR_LOAD:
    db "Loading Kernel...",0
STR_BOOT:
    db "Boot Drive: ",0
MEMMAP_INFO:
    db "Getting Memory Map from BIOS.",0
STR_PM:
    db "PROTECTED MODE",0

; kernel config 
%include "config.inc"

;
;;lets put our temporary GDT (Global Descriptor Table) here
;;kernel should move this away
%include "GDT.asm"
;
;;include 16-bit real mode routines (print_string, disk_load, vesa_setup,check_a20)
%include "disk_load_16.asm"
;%include "boot/check_a20_16.asm"
%include "print_string_16.asm"
%include "vesa_setup_16.asm"
;
;;include our routines for switching to 32-bit protected mode
%include "pm.asm"

;include some pm mode helpers
%include "common_pm.asm"

;
;
;
;;get memory map routine
%include "memmap.asm"
;
;;;;;;;;; BOOT 16-bit real ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;;lets start
[bits 16]

idt_real:
    dw 0x3ff	    ; 256 entries, 4b each = 1K
	dd 0		; Real Mode IVT @ 0x0000

boot_16:

    mov [BOOT_DRIVE],dl


    ; hide text-mode cursor
    mov ah,0x01
    mov cx,0x2607
    int 0x10


    ;pr info 
    mov bx, STR_VERSION
    call print_string
    call print_nextline

    ;show bootdrive
    mov bx, STR_BOOT
    call print_string
    mov al,dl
    call print_hex_byte
    call print_nextline


    ; memmap message
    mov bx,MEMMAP_INFO
    call print_string
    call print_nextline

    ;get memory map from bios before we enter 32 bit protected mode
    mov ax,0	; set target address in es:di (0:offset)
    mov es,ax
    mov di,MEMMAP_OFFSET
    call BiosGetMemoryMap    ; this will also put the number of entries 
	                     ; of the memory map at MEMMAP_SIZE_OFFSET

    ;   Load the KERNEL Image
    mov bx, STR_LOAD
    call print_string
    call print_nextline

kernel_load:

    call disk_load_16



    ; init vesa on last iteration!
    mov ax,[KERNEL_CHUNK]
    cmp ax,(CHUNKS_TO_LOAD-1)
    jne skip_vesa_init

	%ifndef FOOLOS_CONSOLE
	call VesaSetup
        %endif

    skip_vesa_init:
   
    call switch_to_pm
;
;;;;;;;;; BOOT 32-bit protected mode;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
[bits 32]
boot_32_pm:

    ;Fast A20 Gate:
    ;http://wiki.osdev.org/A20_Line
    in al, 0x92
    or al, 2
    out 0x92, al

    ; tell the world we are protected
    ;mov ebx,STR_PM
    ;mov ecx,2*23*80
    ;call print_string_pm

    ;increment chunk number
    mov ax,[KERNEL_CHUNK]
    add ax,1
    mov [KERNEL_CHUNK],ax

    ;check if all chunkgs loaded (hardcoded to 0x6 for a start)
    ;each chunk is 0x8000 * 15 bytes 
    cmp ax,CHUNKS_TO_LOAD
    je finish_load

    ; show KERNEL CHUNK value
;    push edx ; persist edx for some reason!? 
;    mov edx,0
;    mov dx,[KERNEL_CHUNK]
;    mov ecx,2*24*80
;    call print_hex_pm
;    pop edx


	; here we actually do copy the chunk into ext mem!
	mov eax,[LOADER_TARGET]
	mov ebx,0x18000

	copy_next_byte:

	    mov ecx,[ebx]
	    mov [eax],ecx

	    inc eax
	    inc ebx
	    cmp ebx, 0x90000

	jne copy_next_byte
	mov [LOADER_TARGET],eax ;persist next target address


    ; and now go back to real! (first 16bit protected!)
    jmp CODE16_SEG:reinit_16

    finish_load:
    ;

    ; call kernel!
    mov eax,0		;tell the kernel 
			; we are the booting processor
    jmp 0x100000         ;jump into our Kernel!


[bits 16]
reinit_16: ;16 bit protected mode

    mov eax,DATA16_SEG
    mov ds,eax
    mov es,eax
    mov fs,eax
    mov gs,eax
    mov ss,eax


    mov eax,cr0
    and eax,!0x1    ; Disable protected mode
    mov cr0, eax

    jmp 0:realmode


realmode:

    mov sp,0x07bff
    mov bp,sp

    mov ax,0
    mov ds,ax
    mov es,ax
    mov fs,ax
    mov gs,ax
    mov ss,ax

    lidt [idt_real]

    sti
    jmp kernel_load