This repository has been archived by the owner on Nov 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot.asm
112 lines (99 loc) · 1.86 KB
/
boot.asm
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
[BITS 16]
[SECTION .bootsector]
extern boot_main
extern e820_table_size
boot:
jmp 0:start
start:
xor ax, ax
mov ds, ax
mov ss, ax
mov esp, 0x7c00
mov [saved_dl], dl
a20_loop:
; test if A20 is enabled
mov si, 0x500
mov ax, 0xdead
mov [si], ax
mov ax, 0xffff
mov es, ax
mov di, 0x510
mov ax, 0xbeef
mov [es:di], ax
mov ax, [si]
mov bx, 0xdead
cmp ax, bx
je a20_done
; Enable A20 using BIOS (todo: try more methods of enabling)
mov ax, 0x2401
int 0x15
jmp a20_loop
a20_done:
xor ax, ax
mov es, ax
mov di, 0x8000
mov ebx, 0
e820_loop:
mov dword [es:di + 20], 1
mov eax, 0xE820
mov ecx, 24
mov edx, 0x534D4150
int 0x15
jc e820_done
add di, 24
inc byte [e820_size]
test ebx, ebx
jz e820_done
jmp e820_loop
e820_done:
mov dl, [saved_dl]
; Load the second stage
mov si, disk_packet
mov ah, 0x42
int 0x13
; Start protected mode
cli
lgdt [gdtr]
mov eax, cr0
or eax, 1
mov cr0, eax
jmp 0x08:reload_segs
[BITS 32]
reload_segs:
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
mov al, [e820_size]
mov [e820_table_size], al
cld
; Go into C code!
call boot_main
loop:
jmp loop
align 4, db 0
disk_packet:
db 16 ; packet size
db 0
dw 64 ; # of sectors
dw 0 ; offset of buffer
dw 0x1000 ; segment of buffer
dd 1 ; lower 32 bits of LBA
dd 0 ; upper 16 bits of LBA
saved_dl:
db 0
e820_size:
db 0
gdt:
db 0, 0, 0, 0, 0, 0, 0, 0
db 0xff, 0xff, 0x00, 0x00, 0x00, 0x9A, 0xCF, 0x00 ; code segment
db 0xff, 0xff, 0x00, 0x00, 0x00, 0x92, 0xCF, 0x00 ; data segment
gdtr:
dw 23
dd gdt
; End the boot sector
times 510 - ($-boot) db 0
db 0x55
db 0xaa