/
mbr.asm
72 lines (54 loc) · 1.75 KB
/
mbr.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
[org 0x7c00]
KERNEL_OFFSET equ 0x1000 ; The same one we used when linking the kernel
mov [BOOT_DRIVE], dl ; Remember that the BIOS sets us the boot drive in 'dl' on boot
mov bp, 0xFFFF
mov sp, bp
mov bx, MSG_16BIT_MODE
call print16
call print16_nl
call enable_a20_line ; Enable A20 line
call load_kernel ; Read the kernel from disk
call switch_to_32bit ; Disable interrupts, load GDT, etc. Finally jumps to 'BEGIN_PM'
jmp $ ; Never executed
%include "boot/print-16bit.asm"
%include "boot/print-32bit.asm"
%include "boot/disk.asm"
%include "boot/gdt.asm"
%include "boot/switch-to-32bit.asm"
[bits 16]
load_kernel:
mov bx, MSG_LOAD_KERNEL
call print16
call print16_nl
mov bx, 0x1000 ; Read from disk and store in 0x1000
mov dh, 53
mov dl, [BOOT_DRIVE]
call disk_load
ret
enable_a20_line:
cli
in al, 0x92 ; Read the System Control Port B (8042 output port)
or al, 0x02 ; Set bit 1 (A20 gate) to enable A20 line
out 0x92, al ; Write the modified value back to System Control Port B
sti
ret
[bits 32]
BEGIN_32BIT:
pusha
mov esi, 0x1000 ; Source address
mov edi, 0x100000 ; Destination address
mov ecx, 0xFFFF ; Length of data to move
cld ; Set the direction flag to forward
rep movsb ; Move the data from source to destination
popa
mov ebx, MSG_32BIT_MODE
call print32
call 0x1000 ; Give control to the kernel
jmp $ ; Stay here when the kernel returns control to us (if ever)
BOOT_DRIVE db 0 ; It is a good idea to store it in memory because 'dl' may get overwritten
MSG_16BIT_MODE db "Started in 16-bit Real Mode", 0
MSG_32BIT_MODE db "Landed in 32-bit Protected Mode", 0
MSG_LOAD_KERNEL db "Loading kernel into memory", 0
; padding
times 510 - ($-$$) db 0
dw 0xaa55