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