]> git.dujemihanovic.xyz Git - nameless-os.git/blobdiff - boot/x86/stage3/loader.s
Add C code for enabling paging in bootloader
[nameless-os.git] / boot / x86 / stage3 / loader.s
index b67d33894099660760e7c63fc4d471c8090f3507..9ed71021c5d42b291033df366f9231f3b8522b44 100644 (file)
@@ -1,7 +1,11 @@
 bits 16
 cpu 686
-org 0x1800
 
+extern enable_paging
+extern map_range
+extern set_up_page_directory
+
+section .text
 %include "../fat32/fat32-structs.s"
 
 %macro print 1
@@ -11,14 +15,20 @@ org 0x1800
        pop si
 %endmacro
 
+extern __STACK_BOTTOM__
+
+global _start
 _start:
+       mov sp, __STACK_BOTTOM__
        mov [BOOT_DRIVE], dl
        call enable_unreal
+       print begin
        call check_a20
        jc .a20_enabled
        call enable_a20
        jnc .a20_enable_fail
 .a20_enabled:
+       print a20_enabled
        call get_1st_data_sec
        mov ax, 0x1000
        mov es, ax
@@ -52,12 +62,23 @@ _start:
 .kernel_found:
        pop si
        pop cx
+       print kernel_found
        mov ax, [es:di+dir_entry.firstclushi]
        shl eax, 16
        mov ax, [es:di+(dir_entry.firstcluslo)]
+       call print_dword
        mov edi, 0x100000
+       print kernel_loading
        call read_clus_chain_unreal ; load kernel
+       print kernel_loaded
        
+       call get_e820_map
+       mov ebx, ecx
+       mov eax, 24
+       mul ecx
+       add eax, 20
+       sub sp, ax
+
        cli
        lgdt [gdt]
        mov eax, cr0
@@ -139,28 +160,71 @@ memcpy:
        pop esi
        ret
 
-
 %include "unreal.s"
 %include "a20.s"
 %include "../fat32/fat32.s"
 %include "gdt.s"
 %include "print.s"
+%include "e820.s"
 
-in_protected:
 bits 32
+section .text
+in_protected:
        mov ax, 0x10
        mov ds, ax
        mov es, ax
        mov ss, ax
        mov fs, ax
        mov gs, ax
-       call 0x100000
+
+       push dword 0
+       push 0xc0003000
+       push 0xc0000000
+       push 0x103000
+       push 0x100000
+       call map_range
+       cmp eax, 0
+       jne .error
+       add esp, 20
+       push dword 2
+       push 0xc0009000
+       push 0xc0003000
+       push 0x109000
+       push 0x103000
+       call map_range
+       cmp eax, 0
+       jne .error
+       add esp, 20
+       push dword 2
+       push 0x100000
+       push dword 0
+       push 0x100000
+       push dword 0
+       call map_range
+       cmp eax, 0
+       jne .error
+       add esp, 20
+
+       call set_up_page_directory
+       call enable_paging
+
+       jmp 0xc0000000
+       nop
+.error:
+       mov dword [0xb8000], 0xcf28cf3a
        hlt
        jmp $-1
 
+section .rodata
+
 kernel_name: db "KERNEL  BIN"
+begin: db "Nameless Bootloader revision ", GIT_REVISION, 0xd, 0xa, 0
+a20_enabled: db "A20 has been enabled", 0xd, 0xa, "Searching for kernel...", 0xd, 0xa, 0
 a20_fail: db "Failed to enable A20, giving up!", 0xd, 0xa, 0
 crit_err: db "A critical error occurred, dumping registers now: ", 0xd, 0xa, 0
+kernel_found: db "Found kernel at cluster ", 0
+kernel_loading: db 0xd, 0xa, "Loading kernel...", 0xd, 0xa, 0
+kernel_loaded: db "Kernel successfully loaded.", 0xd, 0xa, "Setting up kernel environment and running kernel...", 0xd, 0xa, 0
 missing_kernel: db "Could not find KERNEL.BIN", 0xd, 0xa, 0
 eax_s: db "EAX: ", 0
 ebx_s: db "EBX: ", 0