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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
| %include "include/boot.inc" section loader vstart=LOADER_BASE_ADDR LOADER_STACK_TOP equ LOADER_BASE_ADDR
GDT_BASE: dd 0x00000000 dd 0x00000000
CODE_DESC: dd 0x0000FFFF dd DESC_CODE_HIGH4
DATA_STACK_DESC: dd 0x0000FFFF dd DESC_DATA_HIGH4
VIDEO_DESC: dd 0x80000007 dd DESC_VIDEO_HIGH4
GDT_SIZE equ $ - GDT_BASE GDT_LIMIT equ GDT_SIZE - 1 times 60 dq 0 SELECTOR_CODE equ (0x0001<<3) + TI_GDT + RPL0 SELECTOR_DATA equ (0x0002<<3) + TI_GDT + RPL0 SELECTOR_VIDEO equ (0x0003<<3) + TI_GDT + RPL0
total_mem_bytes dd 0
gdt_ptr dw GDT_LIMIT dd GDT_BASE
ards_buf times 244 db 0 ards_nr dw 0
loader_start:
xor ebx, ebx mov edx, 0x534d4150 mov di, ards_buf .e820_mem_get_loop: mov eax, 0x0000e820 mov ecx, 20 int 0x15 jc .e820_failed_so_try_e801 add di, cx inc word [ards_nr] cmp ebx, 0 jnz .e820_mem_get_loop
mov cx, [ards_nr] mov ebx, ards_buf xor edx, edx .find_max_mem_area: mov eax, [ebx] add eax, [ebx+8] add ebx, 20 cmp edx, eax jge .next_ards mov edx, eax .next_ards: loop .find_max_mem_area jmp .mem_get_ok
.e820_failed_so_try_e801: mov ax,0xe801 int 0x15 jc .e801_failed_so_try88
mov cx,0x400 mul cx shl edx,16 and eax,0x0000FFFF or edx,eax add edx, 0x100000 mov esi,edx
xor eax,eax mov ax,bx mov ecx, 0x10000 mul ecx add esi,eax mov edx,esi jmp .mem_get_ok
.e801_failed_so_try88: mov ah, 0x88 int 0x15 jc .error_hlt and eax,0x0000FFFF mov cx, 0x400 mul cx shl edx, 16 or edx, eax add edx,0x100000
.mem_get_ok: mov [total_mem_bytes], edx
in al,0x92 or al,0000_0010B out 0x92,al
lgdt [gdt_ptr]
mov eax, cr0 or eax, 0x00000001 mov cr0, eax
jmp dword SELECTOR_CODE:p_mode_start .error_hlt: hlt
[bits 32] p_mode_start: mov ax, SELECTOR_DATA mov ds, ax mov es, ax mov ss, ax mov esp,LOADER_STACK_TOP mov ax, SELECTOR_VIDEO mov gs, ax
mov eax, KERNEL_START_SECTOR mov ebx, KERNEL_BIN_BASE_ADDR mov ecx, 200
call rd_disk_m_32
call setup_page
sgdt [gdt_ptr]
mov ebx, [gdt_ptr + 2] or dword [ebx + 0x18 + 4], 0xc0000000
add dword [gdt_ptr + 2], 0xc0000000
add esp, 0xc0000000
mov eax, PAGE_DIR_TABLE_POS mov cr3, eax
mov eax, cr0 or eax, 0x80000000 mov cr0, eax
lgdt [gdt_ptr]
jmp SELECTOR_CODE:enter_kernel enter_kernel:
call kernel_init mov esp, 0xc009f000 jmp KERNEL_ENTRY_POINT
kernel_init: xor eax, eax xor ebx, ebx xor ecx, ecx xor edx, edx
mov dx, [KERNEL_BIN_BASE_ADDR + 42] mov ebx, [KERNEL_BIN_BASE_ADDR + 28] add ebx, KERNEL_BIN_BASE_ADDR mov cx, [KERNEL_BIN_BASE_ADDR + 44] .each_segment: cmp byte [ebx + 0], PT_NULL je .PTNULL
push dword [ebx + 16] mov eax, [ebx + 4] add eax, KERNEL_BIN_BASE_ADDR push eax push dword [ebx + 8] call mem_cpy add esp,12 .PTNULL: add ebx, edx loop .each_segment ret
mem_cpy: cld push ebp mov ebp, esp push ecx mov edi, [ebp + 8] mov esi, [ebp + 12] mov ecx, [ebp + 16] rep movsb
pop ecx pop ebp ret
setup_page:
mov ecx, 4096 mov esi, 0 .clear_page_dir: mov byte [PAGE_DIR_TABLE_POS + esi], 0 inc esi loop .clear_page_dir
.create_pde: mov eax, PAGE_DIR_TABLE_POS add eax, 0x1000 mov ebx, eax
or eax, PG_US_U | PG_RW_W | PG_P mov [PAGE_DIR_TABLE_POS + 0x0], eax mov [PAGE_DIR_TABLE_POS + 0xc00], eax sub eax, 0x1000 mov [PAGE_DIR_TABLE_POS + 4092], eax
mov ecx, 256 mov esi, 0 mov edx, PG_US_U | PG_RW_W | PG_P .create_pte: mov [ebx+esi*4],edx add edx,4096 inc esi loop .create_pte
mov eax, PAGE_DIR_TABLE_POS add eax, 0x2000 or eax, PG_US_U | PG_RW_W | PG_P mov ebx, PAGE_DIR_TABLE_POS mov ecx, 254 mov esi, 769 .create_kernel_pde: mov [ebx+esi*4], eax inc esi add eax, 0x1000 loop .create_kernel_pde ret
rd_disk_m_32:
mov esi,eax mov di,cx
mov dx,0x1f2 mov al,cl out dx,al
mov eax,esi
mov dx,0x1f3 out dx,al
mov cl,8 shr eax,cl mov dx,0x1f4 out dx,al
shr eax,cl mov dx,0x1f5 out dx,al
shr eax,cl and al,0x0f or al,0xe0 mov dx,0x1f6 out dx,al
mov dx,0x1f7 mov al,0x20 out dx,al
.not_ready: nop in al,dx and al,0x88 cmp al,0x08 jnz .not_ready
mov ax, di
mov dx, 256 mul dx mov cx, ax mov dx, 0x1f0 .go_on_read: in ax,dx mov [ebx], ax add ebx, 2
loop .go_on_read ret
|