summaryrefslogtreecommitdiff
path: root/stage1/stage1.S
blob: e11e36bd56a00484b13bf63b3b461ee2d824dfe5 (plain)
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
.intel_syntax noprefix
.code16

.extern _stack_start
.extern _stack_end

.global stage1_sector

.section .stage1, "ax"

stage1_sector:
	xor ax, ax
	
	mov ah, 0x0e
	mov al, 'C'
	mov bh, 0x0
	mov bl, 0x0

	int 0x10
	
	cli

	lgdt gdtr
	
	mov eax, cr0
	or eax, (1 << 0)
	mov cr0, eax

	jmp 0x8:_32_start
	
	cli
	hlt

.code32
_32_start:
	mov eax, 0x10

	mov ds, ax
	mov ss, ax
	mov es, ax
	mov fs, ax
	mov gs, ax
	
	jmp 0x8:_32_entry

	cli
	hlt
	
_32_entry:
	mov eax, offset _stack_start
	mov ebp, eax
	mov eax, offset _stack_end
	mov esp, eax 

	call centry

	cli
	hlt

.code16
.section .stage1_ro, "aw"

gdt:
	.quad 0x0 // Null seg
	
	.word 0xFFFF // Code seg
	.word 0x0000
	.byte 0x0
	.byte 0b10011011 // A=1 RW=1 DC=0 E=1 S=1 DPL=00 P=1
	.byte 0b11001111
	.byte 0x0000

	.word 0xFFFF // Data Seg
	.word 0x0000
	.byte 0x0
	.byte 0b10010011 // A=1 RW=1 DC=0 E=0 S=1 DPL=00 P=1
	.byte 0b11001111
	.byte 0x0000

end_gdt:

gdtr:
	.word end_gdt - gdt - 1
	.long gdt