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
|
; asmsyntax=nasm
[org 0x7c00]
[bits 16]
boot:
cli
mov ax, 0x8000
mov ss, ax
mov sp, 0xffff
push cs
pop ds
xor bx, bx
mov ah, 0x0e
mov si, message
lodsb
.lewp:
int 0x10
lodsb
cmp al, 0
jne .lewp
; Enable A20
mov ax, 0x2401
int 0x15
; HACK: Load the ELF kernel at 0xf000. Assuming that the first
; LOAD header has a file offset of 0x1000, this puts _start
; at 0x10000 which we jump to later.
; This is all quite rickety.
mov bx, 0xf00
mov es, bx
xor bx, bx
mov cx, word [cur_lba]
.sector_loop:
call convert_lba_to_chs
mov ah, 0x02 ; cmd 0x02 - Read Disk Sectors
mov al, 1 ; 1 sector at a time
mov dl, 0 ; drive 0 (fd0)
int 0x13
jc fug
mov ah, 0x0e
mov al, '.'
int 0x10
inc word [cur_lba]
mov cx, word [cur_lba]
cmp cx, 600
jz .sector_loop_end
mov bx, es
add bx, 0x20
mov es, bx
xor bx, bx
jmp .sector_loop
.sector_loop_end:
call durk
; Turn off the floppy motor.
mov dx, 0x3f2
xor al, al
out dx, al
; Let's look at the ELF header.
mov bx, 0xf00
mov fs, bx
cmp [fs:0], dword 0x464c457f ; ELF magic: { 0x7f "ELF" }
jne fug
cmp [fs:24], dword 0x10000 ; Entry should be 0x10000
jne fug
mov ebx, dword [fs:28] ; EBX <- program header table
mov ecx, dword [fs:44] ; ECX <- program header count
; Let's find the BSS and clear it.
parse_program_header:
cmp [fs:ebx], dword 0x1 ; Is Load segment?
jne .next
cmp [fs:ebx+24], dword 0x6 ; Is read+write but not execute?
jne .next
mov edi, [fs:ebx+8] ; EDI <- p_vaddr
add edi, [fs:ebx+16] ; skip over 'p_filesz' bytes (leave them intact)
push ecx
sub edi, [fs:ebx+16] ; skip over 'p_filesz' bytes (see above)
; Since we're in 16-bit real mode, create a segment address.
mov eax, edi
shr eax, 4
mov es, ax
and edi, 0xf
mov ecx, [fs:ebx+20] ; ECX <- p_memsz
xor al, al
rep stosb
pop ecx
.next:
add ebx, 32
loop parse_program_header
; Okay we're all set to go!
lets_go:
lgdt [cs:test_gdt_ptr]
mov eax, cr0
or al, 1
mov cr0, eax
jmp 0x08:pmode
durk:
push cs
pop ds
xor bx, bx
mov ah, 0x0e
mov si, msg_sectors_loaded
lodsb
.lewp:
int 0x10
lodsb
cmp al, 0
jne .lewp
ret
pmode:
[bits 32]
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
mov esp, 0x4000
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
xor ebp, ebp
xor esi, esi
xor edi, edi
jmp 0x10000
hlt
test_gdt_ptr:
dw (test_gdt_end-test_gdt)
dd test_gdt
test_gdt:
dd 0
dd 0
dd 0x0000ffff
dd 0x00cf9a00
dd 0x0000ffff
dd 0x00cf9200
dd 0
dd 0
dd 0
dd 0
test_gdt_end:
[bits 16]
fug:
xor bx, bx
mov ah, 0x0e
mov si, fug_message
lodsb
.lewp:
int 0x10
lodsb
cmp al, 0
jne .lewp
cli
hlt
; Input:
;
; AX = LBA
;
; Output:
;
; CX and DH = C/H/S address formatted for Int13,2
; CL = sector (LBA % sectors_per_track) + 1
;
; 1.44M floppy stats:
; (sectors_per_track: 18)
; (heads: 2)
; (sectors: 2880)
convert_lba_to_chs:
mov ax, cx
; AX = LBA/spt, DX = LBA%spt
xor dx, dx
div word [sectors_per_track]
; CL = sector (LBA % sectors_per_track) + 1
mov cl, dl
inc cl
; CH = track (LBA / sectors_per_track) / heads
mov ch, al
shr ch, 1
; AX = (LBA/spt)/heads, DX = (LBA/spt)%heads
xor dx, dx
div word [heads]
; DH = sector (LBA / sectors_per_track) % heads
mov dh, dl
ret
cur_lba:
dw 1
sectors_per_track:
dw 18
heads:
dw 2
msg_sectors_loaded:
db "done!", 0x0d, 0x0a, 0
message:
db "Loading kernel", 0
fug_message:
db "FUG!", 0x0d, 0x0a, 0
times 510-($-$$) db 0
dw 0xaa55
|