-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootloader-kernel.asm
More file actions
170 lines (139 loc) · 3.01 KB
/
Copy pathbootloader-kernel.asm
File metadata and controls
170 lines (139 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
; Aurora OS Bootloader with Kernel Loading
; Loads Aurora kernel and switches to protected mode
org 0x7C00
bits 16
start:
jmp main
times 3-($-$$) db 0
main:
; Set up segments and stack
cli
xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
mov sp, 0x7C00
sti
; Save boot drive
mov [boot_drive], dl
; Clear screen
mov ax, 0x0003
int 0x10
mov ax, 0x0600
mov bh, 0x1F
xor cx, cx
mov dx, 0x184F
int 0x10
; Display loading message
mov ah, 0x02
mov bh, 0
mov dh, 5
mov dl, 5
int 0x10
mov si, loading_msg
call print_str
; Load kernel from disk sectors 2-5
mov bx, 0x1000 ; Load address
mov ah, 0x02 ; Read function
mov al, 4 ; Number of sectors
mov ch, 0 ; Cylinder 0
mov cl, 2 ; Sector 2
mov dh, 0 ; Head 0
mov dl, [boot_drive]
int 0x13
jc disk_error
; Skip signature check for now
; Just assume kernel loaded successfully
; Success - switch to protected mode
mov si, success_msg
call print_str
call switch_to_pm
; Shouldn't reach here
jmp $
disk_error:
mov si, disk_err_msg
call print_str
jmp reboot
invalid_kernel:
mov si, kernel_err_msg
call print_str
jmp reboot
reboot:
mov si, reboot_msg
call print_str
call wait_key
int 0x19
; Switch to protected mode
switch_to_pm:
cli
lgdt [gdt_descriptor]
mov eax, cr0
or eax, 1
mov cr0, eax
jmp CODE_SEG:protected_mode
bits 32
protected_mode:
; Set up segments
mov ax, DATA_SEG
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
mov esp, 0x90000
; Enable A20 line
in al, 0x92
or al, 2
out 0x92, al
; Jump to kernel
jmp 0x1000
; Halt if kernel returns
cli
hlt
print_str:
lodsb
test al, al
jz .done
mov ah, 0x0E
int 0x10
jmp print_str
.done:
ret
wait_key:
mov ah, 0
int 0x16
ret
; Messages
loading_msg: db "🌠 Loading Aurora kernel...", 0x0D, 0x0A, 0
success_msg: db "✅ Kernel loaded! Switching...", 0x0D, 0x0A, 0
disk_err_msg: db "❌ Disk error!", 0x0D, 0x0A, 0
kernel_err_msg: db "❌ Invalid kernel!", 0x0D, 0x0A, 0
reboot_msg: db "Press any key to reboot...", 0x0D, 0x0A, 0
; GDT
gdt_start:
gdt_null:
dd 0
dd 0
gdt_code:
dw 0xFFFF ; Limit
dw 0 ; Base
db 0
db 10011010b ; Present, Ring 0, Code, Readable
db 11001111b ; Granularity, 32-bit
db 0
gdt_data:
dw 0xFFFF ; Limit
dw 0 ; Base
db 0
db 10010010b ; Present, Ring 0, Data, Writable
db 11001111b ; Granularity, 32-bit
db 0
gdt_end:
gdt_descriptor:
dw gdt_end - gdt_start - 1
dd gdt_start
CODE_SEG equ gdt_code - gdt_start
DATA_SEG equ gdt_data - gdt_start
boot_drive: db 0
times 510 - ($ - $$) db 0
dw 0xAA55