]> git.dujemihanovic.xyz Git - nameless-os.git/blob - boot/x86/stage3/loader.s
9ed71021c5d42b291033df366f9231f3b8522b44
[nameless-os.git] / boot / x86 / stage3 / loader.s
1 bits 16
2 cpu 686
3
4 extern enable_paging
5 extern map_range
6 extern set_up_page_directory
7
8 section .text
9 %include "../fat32/fat32-structs.s"
10
11 %macro print 1
12 push si
13 mov si, %1
14 call print_str
15 pop si
16 %endmacro
17
18 extern __STACK_BOTTOM__
19
20 global _start
21 _start:
22 mov sp, __STACK_BOTTOM__
23 mov [BOOT_DRIVE], dl
24 call enable_unreal
25 print begin
26 call check_a20
27 jc .a20_enabled
28 call enable_a20
29 jnc .a20_enable_fail
30 .a20_enabled:
31 print a20_enabled
32 call get_1st_data_sec
33 mov ax, 0x1000
34 mov es, ax
35 mov eax, BPB_RootClus
36 xor di, di
37 call read_cluster_chain
38 jc critical_error
39
40 push cx
41 push si
42 .find_kernel:
43 mov si, kernel_name
44 cmp byte [es:di], 0 ; end of root directory
45 je .kernel_missing
46 cmp byte [es:di], 0xe5 ; unused entry
47 je .increment
48 mov cx, 11
49 push si
50 push di
51 repe cmpsb
52 pop di
53 pop si
54 je .kernel_found
55 .increment:
56 add di, 32
57 jno .find_kernel
58 mov bx, es
59 add bx, 0x1000
60 mov es, bx
61 jmp .find_kernel
62 .kernel_found:
63 pop si
64 pop cx
65 print kernel_found
66 mov ax, [es:di+dir_entry.firstclushi]
67 shl eax, 16
68 mov ax, [es:di+(dir_entry.firstcluslo)]
69 call print_dword
70 mov edi, 0x100000
71 print kernel_loading
72 call read_clus_chain_unreal ; load kernel
73 print kernel_loaded
74
75 call get_e820_map
76 mov ebx, ecx
77 mov eax, 24
78 mul ecx
79 add eax, 20
80 sub sp, ax
81
82 cli
83 lgdt [gdt]
84 mov eax, cr0
85 or al, 1
86 mov cr0, eax
87 jmp 0x8:in_protected
88 .kernel_missing:
89 print missing_kernel
90 jmp .halt
91
92 .a20_enable_fail:
93 print a20_fail
94 .halt:
95 hlt
96 jmp $-1
97
98 ; eax - start cluster
99 ; edi - address to load to
100 read_clus_chain_unreal:
101 push eax
102 push edi
103 .loop:
104 push di
105 push es
106 push ax
107 xor di, di
108 mov ax, 0x1000
109 mov es, ax
110 pop ax
111 call read_cluster
112 pop es
113 pop di
114 jc .done
115 push esi
116 push eax
117 push ebx
118 push ecx
119 mov esi, 0x10000
120 xor ebx, ebx
121 movzx eax, word BPB_BytsPerSec
122 movzx bx, byte BPB_SecPerClus
123 mul ebx
124 mov ecx, eax
125 call memcpy
126 add edi, ecx
127 pop ecx
128 pop ebx
129 pop eax
130 pop esi
131 cmp eax, 0xffffff7
132 jl .loop
133 .done:
134 pop edi
135 pop eax
136 ret
137
138 ; esi - copy from
139 ; edi - copy to
140 ; ecx - bytes to copy
141 memcpy:
142 push esi
143 push edi
144 push ecx
145 push dx
146
147 .loop:
148 mov dl, [esi]
149 mov [edi], dl
150 dec ecx
151 cmp ecx, 0
152 je .done
153 inc esi
154 inc edi
155 jmp .loop
156 .done:
157 pop dx
158 pop ecx
159 pop edi
160 pop esi
161 ret
162
163 %include "unreal.s"
164 %include "a20.s"
165 %include "../fat32/fat32.s"
166 %include "gdt.s"
167 %include "print.s"
168 %include "e820.s"
169
170 bits 32
171 section .text
172 in_protected:
173 mov ax, 0x10
174 mov ds, ax
175 mov es, ax
176 mov ss, ax
177 mov fs, ax
178 mov gs, ax
179
180 push dword 0
181 push 0xc0003000
182 push 0xc0000000
183 push 0x103000
184 push 0x100000
185 call map_range
186 cmp eax, 0
187 jne .error
188 add esp, 20
189 push dword 2
190 push 0xc0009000
191 push 0xc0003000
192 push 0x109000
193 push 0x103000
194 call map_range
195 cmp eax, 0
196 jne .error
197 add esp, 20
198 push dword 2
199 push 0x100000
200 push dword 0
201 push 0x100000
202 push dword 0
203 call map_range
204 cmp eax, 0
205 jne .error
206 add esp, 20
207
208 call set_up_page_directory
209 call enable_paging
210
211 jmp 0xc0000000
212 nop
213 .error:
214 mov dword [0xb8000], 0xcf28cf3a
215 hlt
216 jmp $-1
217
218 section .rodata
219
220 kernel_name: db "KERNEL BIN"
221 begin: db "Nameless Bootloader revision ", GIT_REVISION, 0xd, 0xa, 0
222 a20_enabled: db "A20 has been enabled", 0xd, 0xa, "Searching for kernel...", 0xd, 0xa, 0
223 a20_fail: db "Failed to enable A20, giving up!", 0xd, 0xa, 0
224 crit_err: db "A critical error occurred, dumping registers now: ", 0xd, 0xa, 0
225 kernel_found: db "Found kernel at cluster ", 0
226 kernel_loading: db 0xd, 0xa, "Loading kernel...", 0xd, 0xa, 0
227 kernel_loaded: db "Kernel successfully loaded.", 0xd, 0xa, "Setting up kernel environment and running kernel...", 0xd, 0xa, 0
228 missing_kernel: db "Could not find KERNEL.BIN", 0xd, 0xa, 0
229 eax_s: db "EAX: ", 0
230 ebx_s: db "EBX: ", 0
231 ecx_s: db "ECX: ", 0
232 edx_s: db "EDX: ", 0
233 esi_s: db "ESI: ", 0
234 edi_s: db "EDI: ", 0
235 cs_s: db "CS: ", 0
236 ds_s: db "DS: ", 0
237 es_s: db "ES: ", 0
238 ss_s: db "SS: ", 0
239 space: db " ", 0
240 hex_delm: db "0x", 0
241 newline: db 0xd, 0xa, 0