forked from anshugupta673/x86_64-decOS-MSC-KIIT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.asm
233 lines (186 loc) · 3.44 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
[BITS 16]
[ORG 0x7e00]
start:
mov [DriveId],dl
mov eax,0x80000000
cpuid
cmp eax,0x80000001
jb NotSupport
mov eax,0x80000001
cpuid
test edx,(1<<29)
jz NotSupport
test edx,(1<<26)
jz NotSupport
LoadKernel:
mov si,ReadPacket
mov word[si],0x10
mov word[si+2],100
mov word[si+4],0
mov word[si+6],0x1000
mov dword[si+8],6
mov dword[si+0xc],0
mov dl,[DriveId]
mov ah,0x42
int 0x13
jc ReadError
LoadUser:
mov si,ReadPacket
mov word[si],0x10
mov word[si+2],10
mov word[si+4],0
mov word[si+6],0x2000
mov dword[si+8],106
mov dword[si+0xc],0
mov dl,[DriveId]
mov ah,0x42
int 0x13
jc ReadError
LoadUser2:
mov si,ReadPacket
mov word[si],0x10
mov word[si+2],10
mov word[si+4],0
mov word[si+6],0x3000
mov dword[si+8],116
mov dword[si+0xc],0
mov dl,[DriveId]
mov ah,0x42
int 0x13
jc ReadError
LoadUser3:
mov si,ReadPacket
mov word[si],0x10
mov word[si+2],10
mov word[si+4],0
mov word[si+6],0x4000
mov dword[si+8],126
mov dword[si+0xc],0
mov dl,[DriveId]
mov ah,0x42
int 0x13
jc ReadError
GetMemInfoStart:
mov eax,0xe820
mov edx,0x534d4150
mov ecx,20
mov dword[0x9000],0
mov edi,0x9008
xor ebx,ebx
int 0x15
jc NotSupport
GetMemInfo:
add edi,20
inc dword[0x9000]
test ebx,ebx
jz GetMemDone
mov eax,0xe820
mov edx,0x534d4150
mov ecx,20
int 0x15
jnc GetMemInfo
GetMemDone:
TestA20:
mov ax,0xffff
mov es,ax
mov word[ds:0x7c00],0xa200
cmp word[es:0x7c10],0xa200
jne SetA20LineDone
mov word[0x7c00],0xb200
cmp word[es:0x7c10],0xb200
je End
SetA20LineDone:
xor ax,ax
mov es,ax
SetVideoMode:
mov ax,3
int 0x10
cli
lgdt [Gdt32Ptr]
lidt [Idt32Ptr]
mov eax,cr0
or eax,1
mov cr0,eax
jmp 8:PMEntry
ReadError:
NotSupport:
End:
hlt
jmp End
[BITS 32]
PMEntry:
mov ax,0x10
mov ds,ax
mov es,ax
mov ss,ax
mov esp,0x7c00
cld
mov edi,0x70000
xor eax,eax
mov ecx,0x10000/4
rep stosd
mov dword[0x70000],0x71003
mov dword[0x71000],10000011b
mov eax,(0xffff800000000000>>39)
and eax,0x1ff
mov dword[0x70000+eax*8],0x72003
mov dword[0x72000],10000011b
lgdt [Gdt64Ptr]
mov eax,cr4
or eax,(1<<5)
mov cr4,eax
mov eax,0x70000
mov cr3,eax
mov ecx,0xc0000080
rdmsr
or eax,(1<<8)
wrmsr
mov eax,cr0
or eax,(1<<31)
mov cr0,eax
jmp 8:LMEntry
PEnd:
hlt
jmp PEnd
[BITS 64]
LMEntry:
mov rsp,0x7c00
cld
mov rdi,0x200000
mov rsi,0x10000
mov rcx,51200/8
rep movsq
mov rax,0xffff800000200000
jmp rax
LEnd:
hlt
jmp LEnd
DriveId: db 0
ReadPacket: times 16 db 0
Gdt32:
dq 0
Code32:
dw 0xffff
dw 0
db 0
db 0x9a
db 0xcf
db 0
Data32:
dw 0xffff
dw 0
db 0
db 0x92
db 0xcf
db 0
Gdt32Len: equ $-Gdt32
Gdt32Ptr: dw Gdt32Len-1
dd Gdt32
Idt32Ptr: dw 0
dd 0
Gdt64:
dq 0
dq 0x0020980000000000
Gdt64Len: equ $-Gdt64
Gdt64Ptr: dw Gdt64Len-1
dd Gdt64