]> git.dujemihanovic.xyz Git - nameless-os.git/blob - boot/x86/stage3/loader.s
Slightly redo informational strings in stage3
[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 cli
68 lgdt [gdt]
69 mov eax, cr0
70 or al, 1
71 mov cr0, eax
72 jmp 0x8:in_protected
73 .kernel_missing:
74 print missing_kernel
75 jmp .halt
76
77 .a20_enable_fail:
78 print a20_fail
79 .halt:
80 hlt
81 jmp $-1
82
83 ; eax - start cluster
84 ; edi - address to load to
85 read_clus_chain_unreal:
86 push eax
87 push edi
88 .loop:
89 push di
90 push es
91 push ax
92 xor di, di
93 mov ax, 0x1000
94 mov es, ax
95 pop ax
96 call read_cluster
97 pop es
98 pop di
99 jc .done
100 push esi
101 push eax
102 push ebx
103 push ecx
104 mov esi, 0x10000
105 xor ebx, ebx
106 movzx eax, word BPB_BytsPerSec
107 movzx bx, byte BPB_SecPerClus
108 mul ebx
109 mov ecx, eax
110 call memcpy
111 add edi, ecx
112 pop ecx
113 pop ebx
114 pop eax
115 pop esi
116 cmp eax, 0xffffff7
117 jl .loop
118 .done:
119 pop edi
120 pop eax
121 ret
122
123 ; esi - copy from
124 ; edi - copy to
125 ; ecx - bytes to copy
126 memcpy:
127 push esi
128 push edi
129 push ecx
130 push dx
131
132 .loop:
133 mov dl, [esi]
134 mov [edi], dl
135 dec ecx
136 cmp ecx, 0
137 je .done
138 inc esi
139 inc edi
140 jmp .loop
141 .done:
142 pop dx
143 pop ecx
144 pop edi
145 pop esi
146 ret
147
148
149 %include "unreal.s"
150 %include "a20.s"
151 %include "../fat32/fat32.s"
152 %include "gdt.s"
153 %include "print.s"
154
155 in_protected:
156 bits 32
157 mov ax, 0x10
158 mov ds, ax
159 mov es, ax
160 mov ss, ax
161 mov fs, ax
162 mov gs, ax
163 call 0x100000
164 hlt
165 jmp $-1
166
167 kernel_name: db "KERNEL BIN"
168 begin: db "Nameless Bootloader revision ", GIT_REVISION, 0xd, 0xa, 0
169 a20_enabled: db "A20 has been enabled", 0xd, 0xa, "Searching for kernel...", 0xd, 0xa, 0
170 a20_fail: db "Failed to enable A20, giving up!", 0xd, 0xa, 0
171 crit_err: db "A critical error occurred, dumping registers now: ", 0xd, 0xa, 0
172 kernel_found: db "Found kernel at cluster ", 0
173 kernel_loading: db 0xd, 0xa, "Loading kernel...", 0xd, 0xa, 0
174 kernel_loaded: db "Kernel successfully loaded.", 0xd, 0xa, "Setting up kernel environment and running kernel...", 0xd, 0xa, 0
175 missing_kernel: db "Could not find KERNEL.BIN", 0xd, 0xa, 0
176 eax_s: db "EAX: ", 0
177 ebx_s: db "EBX: ", 0
178 ecx_s: db "ECX: ", 0
179 edx_s: db "EDX: ", 0
180 esi_s: db "ESI: ", 0
181 edi_s: db "EDI: ", 0
182 cs_s: db "CS: ", 0
183 ds_s: db "DS: ", 0
184 es_s: db "ES: ", 0
185 ss_s: db "SS: ", 0
186 space: db " ", 0
187 hex_delm: db "0x", 0
188 newline: db 0xd, 0xa, 0