-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootldr.S
280 lines (216 loc) · 5.58 KB
/
bootldr.S
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*****************************************************************************/
/* File: bootldr.S */
/* */
/* Description: This is the boot loader. It should be written at sector 0. */
/* It uses bootlds.ld to map itself at address 0x7C00 */
/* 1. Enumerates and stores E820 map */
/* 2. Loads the kernel boot code from sector 1 at addres 0x5000 */
/* 3. Copies to 0x100000 (1MB) address, turns on protected mode and jumps */
/* to that address. */
/* */
/* Author: Shoily O Rahman <[email protected]> */
/* */
/* Date: Feb 11, 2020 */
/* */
/*****************************************************************************/
.include "krnlsize.S"
.include "krnlconst.S"
.equ KERNEL_SECTORS, ((KERNEL_SIZE + 0x200 -1) / 0x200)
// machine boots in 16bit real mode
.code16
.text
.globl _start
_start:
movb %dl, boot_drive
// clear screen
movb $0, %ah
movb $3, %al
int $0x10
// reading e820 map
xorl %eax, %eax
movl %eax, %ds
movl %eax, %es
movl %eax, %ss
movl %eax, %ebp
// initializing e820 call
movl $0xe820, %eax
xorl %ebx, %ebx
movl $24, %ecx
movl $0x534d4150, %edx
movl $E820_MAP_ADDRESS, %edi
movl $1, 20(%edi)
int $0x15
jc e820_failed
movl $0x534d4150, %edx
cmpl %eax, %edx
jne e820_failed
testl %ebx, %ebx
je e820_failed
e820_next_entry:
//looping through e820 next entries
movl $24, %ecx
addl %ecx, %edi
movl $1, 20(%edi)
movl $0xe820, %eax
movl $0x534d4150, %edx
int $0x15
jc e820_done
jcxz e820_done
testl %ebx, %ebx
je e820_done
cmpl $20, %ecx
jne e820_failed
incl %ebp
jmp e820_next_entry
e820_failed:
// write 'E' on the screen to indicate e820 error
movb $'E', %al
movb $0x0e, %ah
int $0x10
jmp a20_line
e820_done:
// store e820 count in memory
movl %ebp, E820_MAP_COUNT
// write '8' on the screen to indicate e820 success
movb $'8', %al
movb $0x0e, %ah
int $0x10
a20_line:
xorl %edx, %edx
movb boot_drive, %dl
// enable A20 line
cli
movb $0xdd, %al
outb %al, $0x64
sti
// read kernel from second sector
xorw %ax, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %ss
// memory buffer location of disk address packet
movw $dap, %si
xorw %ax, %ax
movw %ax, 4(%si)
movw $KERNEL_SECTORS, %ax
// check kernel can fit into existing memory
cmpw $255, %ax
ja kernel_too_big
// read enough sectors to load kernel
movw %ax, 2(%si)
// size of the disk packet
movw $0x0010, (%si)
// kernel is located in sector number 1. Read it.
movl $1, %ebx
movl %ebx, 8(%si)
movl $0, %ebx
movl %ebx, 12(%si)
// segment of buffer address
movw $0x7e0, 6(%si)
// set stack location at 4K offset
movl $0x1000, %ebp
movl %ebp, %esp
// issue int 13h function 42h to read sector
movb $0x42, %ah
int $0x13
jc lba_read_error
// initialize protected mode
cli
xor %ax, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %ss
sti
// resets disk system
int $0x13
// loads gdt
xorw %ax, %ax
movw %ax, %ds
lgdt gdtdescriptor
// turn on protected mode
movl %cr0, %eax
orl $1, %eax
movl %eax, %cr0
cli
// jump to protected mode
ljmpl *codesegaddr
lba_read_error:
// write 'X' on the screen to indicate read error
movb $'X', %al
movb $0x0e, %ah
int $0x10
jmp loop
kernel_too_big:
// write 'B' on the screen to indicate kernel too big
movb $'B', %al
movb $0x0e, %ah
int $0x10
jmp loop
// protected mode code
.code32
protectedmodeaddress:
movl $data_seg, %eax
movl %eax, %ds
movl %eax, %es
movl %eax, %ss
movl %eax, %fs
movl %eax, %gs
// initialize protected mode stack
movl $PROTECTED_MODE_STACK, %eax
movl %eax, %esp
movl %eax, %ebp
// copy kernel to 1 MB offset
cld
movl $(KERNEL_SECTORS * 0x200 / 4), %ecx
movl $0x7e00, %esi
movl $0x100000, %edi
rep movsl
// call kernel at 1 MB
movl $0x100000, %eax
call *%eax
loop:
jmp loop
.data
boot_drive:
.byte 0
mode:
.byte 0
dap: /* disk address packet */
.long 0
.long 0
.long 0
.long 0
// global descriptor table (GDT)
.p2align 5
gdt:
// offset 0x0 - NULL descriptor
.quad 0
code_seg_addr:
// offset 0x8 - cs should point to this descriptor
.word 0xFFFF
.word 0
.byte 0
.byte 0x9A
.byte 0xCF
.byte 0
data_seg_addr:
// offset 0x10 - ds, ss, es, fs, and gs should point to this descriptor
.word 0xFFFF
.word 0
.byte 0
.byte 0x92
.byte 0xCF
.byte 0
end_of_gdt:
gdtdescriptor:
.word end_of_gdt-gdt-1
.long gdt
codesegaddr:
.long protectedmodeaddress
.word code_seg
.equ code_seg, code_seg_addr-gdt
.equ data_seg, data_seg_addr-gdt
// boot loader signature
.section ".bootsig","a"
.byte 0x55
.byte 0xaa