1
0
mirror of https://gitlab.os-k.eu/os-k-team/os-k.git synced 2023-08-25 14:03:10 +02:00
os-k/boot/loader/loader.asm

186 lines
7.1 KiB
NASM

;=----------------------------------------------------------------------------=;
; OS on Kaleid ;
; ;
; Desc: Kernel Loader for OS/K ;
; (x86_64 architecture only) ;
; ;
; ;
; Copyright © 2018-2021 The OS/K Team ;
; ;
; This file is part of OS/K. ;
; ;
; OS/K is free software: you can redistribute it and/or modify ;
; it under the terms of the GNU General Public License as published by ;
; the Free Software Foundation, either version 3 of the License, or ;
; (at your option) any later version. ;
; ;
; OS/K is distributed in the hope that it will be useful, ;
; but WITHOUT ANY WARRANTY; without even the implied warranty of ;
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;
; GNU General Public License for more details. ;
; ;
; You should have received a copy of the GNU General Public License ;
; along with OS/K. If not, see <https://www.gnu.org/licenses/>. ;
;=----------------------------------------------------------------------------=;
%include "boot/loader/multiboot/header.inc"
%include "boot/loader/multiboot/check.inc"
%include "boot/loader/cpu/cpu32.inc"
%include "boot/loader/mem/management.inc"
%include "boot/loader/io/terminal.inc"
%include "boot/loader/cpu/cpu.inc"
%include "boot/loader/mem/structures.inc"
global BtStartLoader
global BtHeader
extern BtStartKern
[BITS 32]
[section .multiboot]
;; MAGNIFICENT MULTIBOOT HEADER FOR GRUB ------------------------------------ ;;
BtHeader:
ALIGN 4
dd MB_HEADER_MAGIC
dd MB_HEADER_FLAGS
dd CHECKSUM
times 5 dd 0x0 ; The unused section (provided for a.out)
dd MB_VIDEO_MODE
dd MB_VIDEO_WIDTH
dd MB_VIDEO_HEIGHT
dd MB_VIDEO_DEPTH
[section .joke]
;; MAGNIFICENT MULTIBOOT HEADER FOR OUR OS BINARY --------------------------- ;;
db "This program cannot be run in Win32 mode, or DOS mode, or in any operating system mode that owns your fantasy.",0
[section .text]
;;MULTIBOOT POINT ENTRY FOR GRUB -------------------------------------------- ;;
BtStartLoader:
mov esp, KERNEL_STACK ; Setup the stack
push 0 ; Reset EFLAGS
popf
mov [mbInfo], ebx
mov [mbMagic], eax
mov ecx, eax ; For debug
call _loader
add esp, 8 ; Cleanup arguments "A la MIPS"
jmp Die ; Aufwiedersehen, but never used
;; THE HOLES ---------------------------------------------------------------- ;;
; ---------------------------------------------------------------------------- ;
; Prints 'ERR:XX' where 'XX' is the str in AX ;
; ---------------------------------------------------------------------------- ;
Error:
mov word [.code], ax
push esi
mov bl, 0x0c
mov esi, .ergo
call write32
pop esi
jmp Die
.ergo : db 219, 219, 219, " This program cannot be run in Win32 mode, or DOS mode, or in any operating system mode that owns your fantasy (OS/K Loader Error "
.code : db "00"
db ")",0
; ---------------------------------------------------------------------------- ;
; Kills the mind of your computer to get it prostrated ;
; ---------------------------------------------------------------------------- ;
Die:
cli
hlt ; die nooooow
jmp $
;; THE CODE ----------------------------------------------------------------- ;;
; ---------------------------------------------------------------------------- ;
; _loader ;
; ;
; This is the function that initiates the launch of OS/K. It verifies that ;
; Grub has properly loaded this loader and makes the appropriate setup so ;
; that the kernel will work properly ;
; ;
; Why this original design ? Because it works well and prevent us to cause ;
; triple faults by aligment error or too long jumps (yup, it can be a problem);
; ;
; ---------------------------------------------------------------------------- ;
_loader:
jmp lbegin
LOGO db 219, 219, 219, " OS/K", 0
mbInfo dq 0
mbMagic dq 0
lbegin:
call clear ; Clear the screen
push esi ; Print the logo
mov bl, 0x0E
mov esi, LOGO
call write32
pop esi
;; BEGIN OF CHECKLIST
call MB_check ; Check Multiboot State, ERR 01
call Check_cpuid ; Check if cpuid supported, ERR 02
call Is64Bits ; Check if long mode available, ERR 03
call CheckA20 ; Check if A20 is correctly enable, ERR 04
;; BEGIN OF WORK
call Setup_paging ; Enable paging
call Go64 ; Prepare switch into long mode
;call disable_cursor
lgdt [GDT64.pointer]
jmp GDT64.code:_loader64 ; SWITCH
[BITS 64]
x64_K db "64 bits long mode activated !", 0x0A, 0x0D, 0x0
GoKernel db "Launching Kernel...", 0
GoStack db "Initializing the stack...", 0x0A, 0x0D, 0x0
nokernel db 219, 219, 219, " Error 05 : Kernel returns",0
_loader64:
;; Some cleanup
mov ax, 0
mov ss, ax
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
call bitemporize
mov qword [NextTRAM], TRAM+80*4 ; Because we don't want to overwrite
;; Hello world
mov bl, 0x0A
mov esi, x64_K
call write
;; Initialize the stack
mov bl, 0x0F
mov esi, GoStack
call write
call temporize ; clear the pipeline
jmp InitStack
AfterInitStack:
;; Launch the kernel !
mov bl, 0x0F
mov esi, GoKernel
call write
mov rdi, [mbInfo]
mov rsi, [mbMagic]
mov rdx, GDT64.code
call BtStartKern
;; We must never reach this point ------------------------------------------- ;;
call tritemporize ; Let time to see
call clear
mov bl, 0x0c
mov esi, nokernel ; Error 05
call write
jmp Die