Nyanix

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit 81102f3c913b7c0589e72c7421ba17c2d94ba438
parent 41e50a25437192e984cd6f9d463a4db6ffefe4ba
Author: k4m1 <k4m1@proton.intra.silverskin.com>
Date:   Fri,  6 Jul 2018 13:40:06 +0300

Added functionality to enable a20

Diffstat:
src/2nd_stage.s | 168+++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 82 insertions(+), 86 deletions(-)

diff --git a/src/2nd_stage.s b/src/2nd_stage.s @@ -1,65 +1,24 @@ -; Entry of 2nd stage of +; Entry of 2nd stage of the bootloader global _start _start: - cli + ; disable interrupts, adjust Stack pointer cli xor bp, bp mov sp, 0x7e00 - call load_kernel + ; we'll jump to protected mode now. + ; after that, we should _never_ return to real 16-bit mode. + ; closest to that we'll get is virtual 8086 mode, aka + ; emulated 16-bit mode. + ; If we return, we'll hang. call switch_to_pm .hang: cli hlt jmp .hang -FAST_A20_AVAIL -SECTORS db 0 -%include "gdt32table.s" -msg_read_failed db "Failed to read disk.", 0x0A, 0x0D, 0 - -; Function to load kernel from disk -load_kernel: - sti - mov ebx, 0x10000 - mov dh, 0x42 - mov dl, [BOOT_DEVICE_DB] - mov byte [SECTORS], dh - xor ch, ch - xor dh, dh - mov cl, 0x02 -read_start: - push ebp - mov ebp, esp - mov di, 5 -read: - mov ah, 0x02 - mov al, [SECTORS] - int 0x13 - jc retry - sub [SECTORS], al - jz read_done - mov cl, 0x01 - xor dh, 1 - jnz read_start - inc ch - jmp read_start -retry: - xor ah, ah - int 0x13 - dec di - jnz read - mov si, msg_read_failed - call real16_dbg_print -error_hang: - cli - hlt - jmp error_hang -read_done: - mov esp, ebp - pop ebp - ret +%include "src/gdt32table.s" ; Function to print debug messages in 16-bit real mode real16_dbg_print: @@ -74,7 +33,6 @@ real16_dbg_print: ; Function to switch to protected mode switch_to_pm: - cli lgdt [GDT_32_PTR] mov eax, cr0 or eax, 1 @@ -84,6 +42,7 @@ switch_to_pm: ; in 32-bit mode ! jmp GDT_32_CODE:pmode_init +; ============================================================================ ;; 32-bit protected mode ;; ============================================================================ ; [bits 32] ; Function to initialize pmode pmode_init: @@ -98,10 +57,12 @@ pmode_init: mov ebp, 0x90000 mov esp, ebp + ; disable interrupts + cli call pmode -; Function that's in protected 32-bit mode. -; entering to kernel! +; ============================================================================ ; +; Function that's in protected 32-bit mode. ;; This function server as the 'main' function of tthe bootloader. ;; ============================================================================ ; pmode: push ebp mov ebp, esp @@ -109,38 +70,28 @@ pmode: xor eax, eax xor ebx, ebx xor ecx, ecx - mov eax, 0x1000 - - call enable_a20_line - -; Look for kernel signature -search_loop: - inc ecx - cmp eax, 0xfffa - jge notfound - cmp word [eax + ecx], 0x4141 - jne search_loop - ; Apparently kernel was found ! - add ecx, 2 - cmp word [eax + ecx], 0xdead - jne search_loop - add ecx, 2 - cmp word [eax + ecx], 0x4141 - jne search_loop - add ecx, 2 - mov ebx, eax - add ebx, ecx - - jmp ebx - -notfound: - mov eax, 1 -done: + xor edx, edx + + call check_a20 + cmp ax, 0 + je .a20_enabled + call enable_a20 + call check_a20 + cmp ax, 0 + je .a20_enabled + + ; a20 line is not completed + cli + hlt + +.a20_enabled: + mov ax, 0xbeef cli hlt -; Functionality for enabling a20-gate -enable_a20_line: +; ============================================================================ ; +; Functionality for enabling a20-gate ;; ============================================================================ ; +check_a20: push ebp mov ebp, esp ; first gotta check if a20 line is enabled already @@ -152,20 +103,65 @@ enable_a20_line: cmpsd popad jne .enabled - + mov ax, 1 + jmp .ret +.enabled: + xor ax, ax +.ret: + mov esp, ebp + pop ebp + ret + +enable_a20: ; we'll enable a20-gate via keyboard controller. + call kbd_wait + mov al, __KBD_DISABLE + call kbd_send + call kbd_wait + mov al, __KBD_READ_IN + call kbd_send + call kbd_read + push eax + call kbd_wait + mov al, __KBD_WRITE_OUT + call kbd_send + call kbd_wait + pop eax + or al, 2 + call kbd_send + call kbd_wait -.enabled: - mov eax, 1 -.ret: - mov esp, ebp + mov esp, ebp pop ebp ret + +; ============================================================================ ;; Keyboard Control related functionality. ; +; ============================================================================ ; + +__KBD_DISABLE equ 0xAD +__KBD_ENABLE equ 0xAE +__KBD_READ_IN equ 0xD0 +__KBD_WRITE_OUT equ 0xD1 + +kbd_wait: + in al, 0x64 + test al, 2 + jnz kbd_wait + ret +kbd_read: + in al, 0x64 + test al, 1 + jz kbd_read + in al, 0x60 + ret +kbd_send: + out 0x64, al + ret