Debian Codegraph @

kernel/boot/bootsect.asm

raw
1 ; Boot sector, freely inspired by http://jojo.ouvaton.org/dossiers/boot_sector/tutorial02.html
2 ; Sets a GDT and protected mode
3 ; Copy the kernel (or stage2) to 0x1000 (linear)
4 ; Max kernel size is 25600 bytes (50 sectors)
5
6
7 %define BASE 0x100 ; 0x0100:0x0 = 0x1000
8 %define KSIZE 50
9
10 [BITS 16]
11 [ORG 0x0]
12
13 ; Code starts here, jump to our real entry point, skipping print
14 jmp start
15
16 ; print string using BIOS
17 print:
18 push ax
19 push bx
20 .start:
21 lodsb ; ds:si -> al
22 cmp al,0
23 jz .fin
24 mov ah,0x0E ; 0x0e function of BIOS 0x10 interruption
25 mov bx,0x07 ; bx -> attribute, al -> ascii
26 int 0x10
27 jmp .start
28
29 .fin:
30 pop bx
31 pop ax
32 ret
33
34 ; Entry point
35 start:
36 mov [bootdrv],dl ; boot drive
37
38 ; Stack and section initialization at 0x07C0
39 mov ax,0x07C0
40 mov ds,ax
41 mov es,ax
42 mov ax,0x8000 ; stack at 0xFFFF
43 mov ss,ax
44 mov sp, 0xf000
45
46 ; print 'Loading kernel ...'
47 mov si, ldKernel
48 call print
49
50 ; Load kernel
51 xor ax,ax
52 int 0x13
53
54 push es
55 mov ax,BASE
56 mov es,ax
57 mov bx,0
58 mov ah,2
59 mov al,KSIZE
60 mov ch,0
61 mov cl,2
62 mov dh,0
63 mov dl,[bootdrv]
64 int 0x13
65 pop es
66
67 ; GDT pointer
68 mov ax,gdtend ; GDT limit
69 mov bx,gdt
70 sub ax,bx
71 mov word [gdtptr],ax
72
73 xor eax,eax ; GDT linear address
74 xor ebx,ebx
75 mov ax,ds
76 mov ecx,eax
77 shl ecx,4
78 mov bx,gdt
79 add ecx,ebx
80 mov dword [gdtptr+2],ecx
81
82 ; pmode
83 cli
84 lgdt [gdtptr] ; load GDT
85 mov eax, cr0
86 or ax,1
87 mov cr0,eax ; enter Protected Mode
88
89 jmp next
90 next:
91 mov ax,0x10 ; data segment
92 mov ds,ax
93 mov fs,ax
94 mov gs,ax
95 mov es,ax
96 mov ss,ax
97 mov esp,0x9F000
98
99 jmp dword 0x8:0x1000 ; jump to kernel _start at 0x1000 of 'second' selector (*8)
100
101
102 ;--------------------------------------------------------------------
103 bootdrv: db 0
104 ldKernel db "Loading kernel ...",13,10,0
105 ;--------------------------------------------------------------------
106 gdt:
107 db 0,0,0,0,0,0,0,0
108 gdt_cs:
109 db 0xFF,0xFF,0x0,0x0,0x0,10011011b,11011111b,0x0
110 gdt_ds:
111 db 0xFF,0xFF,0x0,0x0,0x0,10010011b,11011111b,0x0
112 gdtend:
113
114 ;--------------------------------------------------------------------
115 gdtptr:
116 dw 0 ; limit
117 dd 0 ; base
118
119
120 ;--------------------------------------------------------------------
121 ;; fill remaining space with NOPs
122 times 510-($-$$) db 144
123 dw 0xAA55