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