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