StupidOS/boot/loader/loader.asm

172 lines
2.9 KiB
NASM
Raw Normal View History

2024-03-26 07:39:40 +00:00
;; File: loader.asm
format binary
2024-03-20 09:48:47 +00:00
include '../common/const.inc'
2024-04-14 05:35:43 +00:00
include '../common/macro.inc'
include 'multiboot.inc'
org LOADER_BASE
use32
2024-03-20 09:48:47 +00:00
jmp _start
align 4
2024-03-20 09:48:47 +00:00
multiboot_header:
mb_header MultibootHeader multiboot_header
_start:
cmp eax, MULTIBOOT_MAGIC
je multiboot
2024-03-20 09:48:47 +00:00
use16
2024-04-02 10:03:47 +00:00
align 2
; =========================================================================
; real mode code
; =========================================================================
2024-03-20 09:48:47 +00:00
push cs
pop ds
2024-04-28 06:41:36 +00:00
mov [drive_number], dl
2024-03-20 09:48:47 +00:00
mov si, msg_stage2
2024-04-28 06:41:36 +00:00
call bios_log
2024-03-20 09:48:47 +00:00
2024-04-02 10:03:47 +00:00
; enable A20 line
2024-03-20 09:48:47 +00:00
call a20_enable
jc .error_a20
2024-05-02 11:34:27 +00:00
xchg bx, bx
2024-04-28 06:41:36 +00:00
; check drive type
; dl <= 0x7F == floppy
; dl >= 0x80 == hard drive
; dl == 0xE0 ~= CD/DVD
; dl <= 0xFF == hard drive
mov dl, [drive_number]
cmp dl, 0x7F
; skip disk extension check
2024-05-28 04:54:10 +00:00
jle @f
2024-04-28 06:41:36 +00:00
; check disk extensions
mov ah, 0x41
mov bx, 0x55AA
int 0x13
jc @f
mov [drive_lba], 1
@@:
; detect filesystem (FAT12/16 or StpdFS)
; load kernel from filesystem
2024-05-02 11:34:27 +00:00
; +---------+--------+---------+
; | bootsec | sect 1 | stpd sb |
; +---------+--------+---------+
2024-05-28 04:54:10 +00:00
; 0 512 1024
;
2024-05-02 11:34:27 +00:00
; for now fat12 is asumed
2024-05-28 04:54:10 +00:00
call fat_load_root
mov si, kernel_fat12_file
call fat_search_root
jc .error_not_found
2024-05-02 11:34:27 +00:00
2024-04-28 06:41:36 +00:00
2024-04-02 10:03:47 +00:00
; fetch memory map from bios
2024-05-02 11:34:27 +00:00
call memory_get_map
jc .error_memory
2024-03-20 09:48:47 +00:00
2024-04-02 10:03:47 +00:00
; video information
2024-03-20 09:48:47 +00:00
call video_setup
2024-04-02 10:03:47 +00:00
; load GDT and enter Protected-Mode
lgdt [gdt_ptr]
; enable protected mode
mov eax, cr0
or al, 1
mov cr0, eax
; reload ds, es, fs, gs, ss
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
jmp 0x8:common32
2024-03-20 09:48:47 +00:00
2024-05-28 04:54:10 +00:00
.error_not_found:
mov si, msg_error_not_found
jmp .error
2024-03-20 09:48:47 +00:00
.error_memory:
mov si, msg_error_memory
jmp .error
.error_a20:
mov si, msg_error_a20
.error:
2024-04-28 06:41:36 +00:00
call bios_log
2024-05-28 04:54:10 +00:00
@@:
2024-03-20 09:48:47 +00:00
hlt
jmp @b
2024-03-20 09:48:47 +00:00
include 'a20.inc'
include '../common/bios.inc'
2024-05-28 04:54:10 +00:00
include '../common/fat12.inc'
include 'disk.inc'
2024-04-28 06:41:36 +00:00
include 'logger.inc'
include 'memory.inc'
include 'video.inc'
include 'gdt.inc'
2024-03-20 09:48:47 +00:00
2024-04-28 06:41:36 +00:00
drive_number rb 1
drive_lba db 0
msg_stage2 db "StupidOS Loader", 0
2024-04-14 05:35:43 +00:00
kernel_fat12_file db "VMSTUPIDSYS", 0
2024-04-28 06:41:36 +00:00
msg_error_a20 db "ERROR: can't enable a20 line", 0
msg_error_memory db "ERROR: can't detect available memory", 0
2024-05-28 11:50:12 +00:00
msg_error_sector db "ERROR: reading sector", CR, LF, 0
2024-05-28 04:54:10 +00:00
msg_error_not_found db "ERROR: kernel not found", 0
2024-03-20 09:48:47 +00:00
use32
; =========================================================================
; protected mode code
; =========================================================================
multiboot:
2024-04-02 10:03:47 +00:00
; https://www.gnu.org/software/grub/manual/multiboot/multiboot.html#Machine-state
; get screen information
; get memory map
; get kernel from module
2024-05-03 10:00:17 +00:00
common32:
2024-04-02 10:03:47 +00:00
2024-05-03 10:00:17 +00:00
mov [0xB8000], dword 0x07690748
; paging
; identity map first 1MB
; map kernel to 0xC0000000
2024-04-28 06:41:36 +00:00
push STPDBOOT_MAGIC
mov eax, 0xC0000000
jmp eax
hang:
hlt
jmp $
2024-03-20 09:48:47 +00:00
_edata:
2024-04-02 10:03:47 +00:00
boot_structure:
align 4096
boot_page_directory:
rb 4096
boot_768_page_table:
rb 4096
2024-03-20 09:48:47 +00:00
_end: