Nyanix

Bootloader
Log | Files | Refs | LICENSE

commit c9028f021b13f2d19e4e4b9f90e3ea473dac25b1
parent 3e744c311e80e461be9bedfc3dcd1cdfb6f46f3b
Author: Dani Frisk <dani@silverskin.fi>
Date:   Sun,  2 Dec 2018 21:21:33 +0200

Big bugfix

Diffstat:
README.md | 2+-
src/boot_early/a20gate.s | 80++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
src/boot_early/boot.s | 3++-
src/boot_early/second_stage.s | 40++++++++++++++++++++--------------------
src/drivers/kbd_ctl.s | 40----------------------------------------
src/main/main_loader.s | 4++--
6 files changed, 70 insertions(+), 99 deletions(-)

diff --git a/README.md b/README.md @@ -1,7 +1,7 @@ # Nyanix Simple bootloader project. Aiming for clean code. -This bootloader is capable of loading up to 64 Megabytes large kernels. +This bootloader is cabable of loading up to 64 Megabytes large kernels. Kernel must be in flat-binary format, at the moment there is no support for ELF, nor any other filetypes. diff --git a/src/boot_early/a20gate.s b/src/boot_early/a20gate.s @@ -18,7 +18,9 @@ ; Check A20 line ; Returns to caller if A20 gate is cleared. ; Continues to A20_on if A20 line is set. +; Written by Elad Ashkcenazi check_a20gate: + xor eax, eax pushad mov esi, 0x012345 mov edi, 0x112345 @@ -27,7 +29,6 @@ check_a20gate: cmpsd popad jne .a20_isenabled - xor eax, eax ret .a20_isenabled: inc eax @@ -38,38 +39,47 @@ check_a20gate: ; Not returning anythin, nor requiring arguments. ; ; =========================================================================== ; toggle_a20_status: - push ebp - mov ebp, esp - - cli - call __kbdctl_wait - mov eax, KBD_DISABLE - call __kbdctl_outb - - call __kbdctl_wait - mov eax, KBD_READ - call __kbdctl_outb - - call __kbdctl_wait - call __kbdctl_inb - push eax - - call __kbdctl_wait - mov eax, KBD_WRITE - call __kbdctl_outb - - call __kbdctl_wait - pop eax - or eax, 2 - call __kbdctl_outb - - call __kbdctl_wait - mov eax, KBD_ENABLE - call __kbdctl_outb - - sti - mov esp, ebp - pop ebp - ret - + cli + + call a20wait + mov al,0xAD + out 0x64,al + + call a20wait + mov al,0xD0 + out 0x64,al + + call a20wait2 + in al,0x60 + push eax + + call a20wait + mov al,0xD1 + out 0x64,al + + call a20wait + pop eax + or al,2 + out 0x60,al + + call a20wait + mov al,0xAE + out 0x64,al + + call a20wait + sti + ret + +a20wait: + in al,0x64 + test al,2 + jnz a20wait + ret + + +a20wait2: + in al,0x64 + test al,1 + jz a20wait2 + ret %endif diff --git a/src/boot_early/boot.s b/src/boot_early/boot.s @@ -13,7 +13,7 @@ org 0x7c00 bits 16 align 4 -%define SECTOR_COUNT 1 +%define SECTOR_COUNT 2 ; clearing out CS jmp 0x0000:start @@ -129,3 +129,4 @@ dw 0xAA55 %include "src/boot_early/second_stage.s" + diff --git a/src/boot_early/second_stage.s b/src/boot_early/second_stage.s @@ -18,9 +18,6 @@ _start: %define kernel_found 1 %define sector_size 0x1000 -; drivers. -%include "src/drivers/kbd_ctl.s" - ; a20 gate related operations. %include "src/boot_early/a20gate.s" @@ -31,6 +28,8 @@ _start: ; Messages ; ; =========================================================================== ; msg_a20_fail db "Failed to enable A20 gate!", 0x0A, 0 +msg_main_loader_ret db "Unknown fatal error with main loader!", 0x0A, 0 +msg_panic db "[!] Panic: ", 0 ; =========================================================================== ; ; boot device backup ; @@ -41,18 +40,21 @@ boot_device_db db 0, 0 ; Main function of second stage entry. ; ; =========================================================================== ; main: - xor ebp, ebp - mov esp, 0x9c00 + xor ebp, ebp + mov esp, 0x9c00 + xor eax, eax mov al, [BOOT_DEVICE] mov [boot_device_db], al call check_a20gate - jnz .a20_enabled + cmp eax, 0 + je .a20_is_enabled call toggle_a20_status call check_a20gate - jnz .a20_enabled + cmp eax, 0 + je .a20_is_enabled ; we apparently failed to enable a20 gate. :( mov esi, msg_a20_fail @@ -60,20 +62,18 @@ main: ; we've enabled a20 gate now, time to start loading ; kernel from the disk. - .a20_enabled: - call main_loader + .a20_is_enabled: + call main_loader + + ; we returned from main loader? + ; that means we've failed hard, and it's time + ; to panic now! + + mov esi, msg_main_loader_ret + call panic ; Fancy little panic function. panic: - lodsb - or al, al - int 0x10 - cmp al, 0 - jne panic - - .hang: - cli - hlt - jmp .hang + cli + hlt - diff --git a/src/drivers/kbd_ctl.s b/src/drivers/kbd_ctl.s @@ -1,40 +0,0 @@ -; ============================================================================= ; -; /drivers/kbd_ctl.s ; -; Copyright (C) 2018, k4m1 <k4m1@protonmail.com> ; -; For full license, see /LICENSE file. ; -; ; -; This file provides functionality needed for interacting with ; -; Keyboard Controller chip (chip 8042). ; -; ============================================================================= ; - -[ BITS 16 ] - -%ifndef __DRIVERS_KBDCTL_H__ -%define __DRIVERS_KBDCTL_H__ 1 - - -%define KBDCTL_PORT 0x64 ; GPIO port for keyboard controller. -%define KBD_DISABLE 0xAD -%define KBD_READ 0xD0 -%define KBD_WRITE 0xD1 -%define KBD_ENABLE 0xAE - -__kbdctl_wait: - in al, KBDCTL_PORT - test al, 2 - jnz __kbdctl_wait - ret - -; Returns one byte at al -__kbdctl_inb: - in al, KBDCTL_PORT - test al, 1 - jz __kbdctl_inb - ret - -; byte to output at al -__kbdctl_outb: - out KBDCTL_PORT, al - ret - -%endif diff --git a/src/main/main_loader.s b/src/main/main_loader.s @@ -21,8 +21,8 @@ sectors_remaining dw 1 ; the amount of sectors to load kernel_sectors_loaded dw 0 main_loader: - push ebp - mov ebp, esp + push ebp + mov ebp, esp xor eax, eax xor ebx, ebx