-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.asm
90 lines (74 loc) · 1.6 KB
/
loader.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
KERNEL_BASE equ 0x800
KERNEL_BASE_PHY equ 0x8000
MEMCHK_NUM_ADDR equ 0x7e00
ADDR_RANGE_DESC_TBL_ADDR equ (MEMCHK_NUM_ADDR + 4)
%include "pm.inc"
org 07c00h
mov ax, cs
mov ds, ax
;save bios info before entering protected mode
mov ebx, 0
mov di, ADDR_RANGE_DESC_TBL_ADDR
MEM_CHK_LOOP:
mov eax, 0xe820
mov ecx, 20
mov edx, 0x534D4150
int 15h
jc MEM_CHK_FAIL
add di, 20
inc dword [MEMCHK_NUM_ADDR]
cmp ebx, 0
jne MEM_CHK_LOOP
jmp MEM_CHK_OK
MEM_CHK_FAIL:
mov dword [MEMCHK_NUM_ADDR], 0
MEM_CHK_OK:
xor ax, ax
xor dl, dl
int 13
mov ax, KERNEL_BASE
mov es, ax
mov ah, 02h
mov al, 16h ; kernel is in 16 sectors
mov ch, 00h
mov cl, 02h
mov dh, 00h
mov dl, 00h
mov bx, 0
int 13h
jmp SWITCH_PM
LABEL_GDT: boot_descriptor 0, 0, 0
LABEL_DESC_FLAT_C: boot_descriptor 0, 0fffffh,DA_32|DA_CR|DA_LIMIT_4K
LABEL_DESC_FLAT_RW: boot_descriptor 0, 0fffffh,DA_32|DA_DRW|DA_LIMIT_4K
LABEL_DESC_VIDEO: boot_descriptor 0B8000h,0ffffh, DA_DRW|DA_DPL0
LABEL_DESC_LDT: boot_descriptor 0, 0, 0
LABEL_DESC_TSS: boot_descriptor 0, 0, 0
gdt_len equ $ - LABEL_GDT
gdt_ptr dw gdt_len - 1
dd LABEL_GDT
selector_flat_c equ LABEL_DESC_FLAT_C - LABEL_GDT
selector_flat_rw equ LABEL_DESC_FLAT_RW - LABEL_GDT
selector_video equ LABEL_DESC_VIDEO - LABEL_GDT + SA_RPL0
SWITCH_PM:
lgdt [gdt_ptr]
cli
in al, 92h
or al, 02h
out 92h, al
mov eax, cr0
or eax, 1
mov cr0, eax
jmp dword selector_flat_c:PM_START
[BITS 32]
PM_START:
mov ax, selector_video
mov gs, ax
mov ax, selector_flat_rw
mov ds, ax
mov es, ax
mov fs, ax
mov ss, ax
mov esp, 0x7c00
jmp KERNEL_BASE_PHY
times 510-($-$$) db 0
dw 0xaa55