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