2024-03-26 07:39:40 +00:00
|
|
|
;; File: loader.asm
|
2024-03-24 08:03:03 +00:00
|
|
|
format binary
|
2024-03-20 09:48:47 +00:00
|
|
|
|
2024-03-24 08:03:03 +00:00
|
|
|
include '../common/const.inc'
|
2024-04-14 05:35:43 +00:00
|
|
|
include '../common/macro.inc'
|
2024-03-24 08:03:03 +00:00
|
|
|
include 'multiboot.inc'
|
|
|
|
|
|
|
|
org LOADER_BASE
|
|
|
|
use32
|
2024-03-20 09:48:47 +00:00
|
|
|
|
|
|
|
jmp _start
|
|
|
|
|
2024-03-24 08:03:03 +00:00
|
|
|
align 4
|
2024-03-20 09:48:47 +00:00
|
|
|
multiboot_header:
|
|
|
|
mb_header MultibootHeader multiboot_header
|
|
|
|
|
2024-07-05 06:16:40 +00:00
|
|
|
;; Function: _start
|
|
|
|
;; Loader entry point.
|
2024-03-20 09:48:47 +00:00
|
|
|
_start:
|
|
|
|
cmp eax, MULTIBOOT_MAGIC
|
2024-03-31 10:27:15 +00:00
|
|
|
je multiboot
|
2024-03-20 09:48:47 +00:00
|
|
|
|
2024-03-24 08:03:03 +00:00
|
|
|
use16
|
2024-04-02 10:03:47 +00:00
|
|
|
align 2
|
2024-03-31 10:27:15 +00:00
|
|
|
|
|
|
|
; =========================================================================
|
|
|
|
; real mode code
|
|
|
|
; =========================================================================
|
2024-03-20 09:48:47 +00:00
|
|
|
push cs
|
|
|
|
pop ds
|
|
|
|
|
2024-06-07 09:25:28 +00:00
|
|
|
mov [uDrive], dl
|
2024-04-28 06:41:36 +00:00
|
|
|
|
2024-06-07 09:25:28 +00:00
|
|
|
mov si, szMsgStage2
|
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-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
|
2024-06-07 09:25:28 +00:00
|
|
|
mov dl, [uDrive]
|
2024-04-28 06:41:36 +00:00
|
|
|
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
|
2024-06-07 09:25:28 +00:00
|
|
|
mov [bDriveLBA], TRUE
|
2024-04-28 06:41:36 +00:00
|
|
|
@@:
|
|
|
|
; detect filesystem (FAT12/16 or StpdFS)
|
|
|
|
; load kernel from filesystem
|
2024-06-14 03:32:46 +00:00
|
|
|
|
|
|
|
; StupidFS layout
|
|
|
|
; +---------+---------+---------....--+----....---+
|
|
|
|
; | bootsec | stpd sb | i-nodes ... | data |
|
2024-06-29 14:29:15 +00:00
|
|
|
; +---------+---------+---------....--+----....---+
|
2024-06-14 03:32:46 +00:00
|
|
|
; 0 512 1024 XXXX XXXX
|
2024-05-28 04:54:10 +00:00
|
|
|
;
|
2024-06-14 03:32:46 +00:00
|
|
|
|
2024-05-02 11:34:27 +00:00
|
|
|
; for now fat12 is asumed
|
2024-06-11 08:34:41 +00:00
|
|
|
|
2024-05-28 04:54:10 +00:00
|
|
|
call fat_load_root
|
|
|
|
|
2024-06-07 09:25:28 +00:00
|
|
|
mov si, szKernelFile
|
2024-05-28 04:54:10 +00:00
|
|
|
call fat_search_root
|
|
|
|
jc .error_not_found
|
2024-06-11 09:53:34 +00:00
|
|
|
mov [pKernelStartFat], ax
|
2024-06-14 03:32:46 +00:00
|
|
|
mov [uKernelSize], ebx
|
|
|
|
|
|
|
|
push ebx
|
|
|
|
mov si, szMsgKernelFound
|
|
|
|
call bios_log
|
2024-06-11 09:53:34 +00:00
|
|
|
|
|
|
|
; load fat
|
|
|
|
xor ax, ax
|
|
|
|
mov al, [FAT_count]
|
|
|
|
mul word [sectors_per_FAT]
|
|
|
|
mov cx, ax
|
|
|
|
mov ax, [reserved_sectors]
|
|
|
|
|
|
|
|
xor bx, bx
|
2024-04-28 06:41:36 +00:00
|
|
|
|
2024-06-11 09:53:34 +00:00
|
|
|
call disk_read_sectors
|
|
|
|
|
|
|
|
; load stage 2
|
|
|
|
mov ax, KERNEL_PRELOAD/0x10
|
|
|
|
mov es, ax
|
|
|
|
mov ax, [pKernelStartFat]
|
|
|
|
xor bx, bx
|
|
|
|
call fat_load_binary
|
|
|
|
|
2024-06-29 14:29:15 +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-07-02 08:32:11 +00:00
|
|
|
call boot_info_print_mmap
|
|
|
|
|
2024-04-02 10:03:47 +00:00
|
|
|
; video information
|
2024-07-05 05:46:04 +00:00
|
|
|
;call video_setup
|
2024-03-20 09:48:47 +00:00
|
|
|
|
2024-04-02 10:03:47 +00:00
|
|
|
; load GDT and enter Protected-Mode
|
|
|
|
lgdt [gdt_ptr]
|
|
|
|
|
|
|
|
; enable protected mode
|
|
|
|
mov eax, cr0
|
2024-07-07 13:48:22 +00:00
|
|
|
or al, CR0_PE
|
2024-04-02 10:03:47 +00:00
|
|
|
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:
|
2024-06-07 09:25:28 +00:00
|
|
|
mov si, szMsgErrorNotFound
|
2024-05-28 04:54:10 +00:00
|
|
|
jmp .error
|
2024-03-20 09:48:47 +00:00
|
|
|
.error_memory:
|
2024-06-07 09:25:28 +00:00
|
|
|
mov si, szMsgErrorMemory
|
2024-03-20 09:48:47 +00:00
|
|
|
jmp .error
|
|
|
|
.error_a20:
|
2024-06-07 09:25:28 +00:00
|
|
|
mov si, szMsgErrorA20
|
2024-03-20 09:48:47 +00:00
|
|
|
.error:
|
2024-04-28 06:41:36 +00:00
|
|
|
call bios_log
|
2024-05-28 04:54:10 +00:00
|
|
|
|
2024-03-31 10:27:15 +00:00
|
|
|
@@:
|
2024-03-20 09:48:47 +00:00
|
|
|
hlt
|
2024-03-31 10:27:15 +00:00
|
|
|
jmp @b
|
2024-03-20 09:48:47 +00:00
|
|
|
|
2024-03-24 08:03:03 +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'
|
2024-03-24 08:03:03 +00:00
|
|
|
include 'memory.inc'
|
|
|
|
include 'video.inc'
|
2024-03-31 10:27:15 +00:00
|
|
|
include 'gdt.inc'
|
2024-07-02 08:32:11 +00:00
|
|
|
include '../common/bootinfo.inc'
|
2024-07-07 13:48:22 +00:00
|
|
|
include '../../kernel/sys/register.inc'
|
2024-07-08 17:10:37 +00:00
|
|
|
include '../../kernel/sys/mmu.inc'
|
2024-03-20 09:48:47 +00:00
|
|
|
|
2024-06-11 08:34:41 +00:00
|
|
|
uDrive rb 1
|
|
|
|
bDriveLBA db FALSE
|
2024-04-28 06:41:36 +00:00
|
|
|
|
2024-06-11 09:53:34 +00:00
|
|
|
pKernelStartFat dw 0
|
2024-06-14 03:32:46 +00:00
|
|
|
uKernelSize dd 0
|
2024-06-11 09:53:34 +00:00
|
|
|
|
|
|
|
szMsgStage2 db "StupidOS Loader", 0
|
2024-06-07 09:25:28 +00:00
|
|
|
szKernelFile db "VMSTUPIDSYS", 0
|
2024-06-14 03:32:46 +00:00
|
|
|
szMsgKernelFound db "Kernel found, size: %x", 0
|
2024-06-07 09:25:28 +00:00
|
|
|
szMsgErrorA20 db "ERROR: can't enable a20 line", 0
|
|
|
|
szMsgErrorMemory db "ERROR: can't detect available memory", 0
|
2024-06-11 09:53:34 +00:00
|
|
|
szMsgErrorSector db "ERROR: reading sector", CR, LF, 0
|
2024-06-07 09:25:28 +00:00
|
|
|
szMsgErrorNotFound db "ERROR: kernel not found", 0
|
2024-03-20 09:48:47 +00:00
|
|
|
|
2024-03-31 10:27:15 +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-03-31 10:27:15 +00:00
|
|
|
|
2024-05-03 10:00:17 +00:00
|
|
|
|
2024-03-31 10:27:15 +00:00
|
|
|
common32:
|
2024-05-03 10:00:17 +00:00
|
|
|
mov [0xB8000], dword 0x07690748
|
2024-06-07 09:25:28 +00:00
|
|
|
|
2024-07-06 13:19:35 +00:00
|
|
|
; move kernel to 0x100000
|
|
|
|
mov ecx, [uKernelSize]
|
|
|
|
mov esi, KERNEL_PRELOAD
|
|
|
|
mov edi, KERNEL_BASE
|
|
|
|
rep movsb
|
|
|
|
|
2024-03-31 10:27:15 +00:00
|
|
|
; identity map first 1MB
|
2024-07-05 08:38:19 +00:00
|
|
|
xor esi, esi
|
|
|
|
xor ecx, ecx
|
|
|
|
mov edi, boot_0_page_table
|
|
|
|
@@:
|
|
|
|
mov edx, esi
|
2024-07-08 17:10:37 +00:00
|
|
|
or edx, (PTE_P or PTE_W)
|
2024-07-05 08:38:19 +00:00
|
|
|
mov [edi], edx
|
|
|
|
add edi, 4
|
|
|
|
add esi, 4096
|
|
|
|
inc ecx
|
|
|
|
cmp ecx, 1024
|
|
|
|
jb @b
|
2024-06-29 14:29:15 +00:00
|
|
|
|
2024-07-08 17:10:37 +00:00
|
|
|
mov dword [boot_page_directory], boot_0_page_table + (PDE_P or PDE_W) ; preset and writable
|
2024-06-14 03:32:46 +00:00
|
|
|
|
2024-07-08 17:10:37 +00:00
|
|
|
mov dword [boot_page_directory + (768 * 4)], boot_0_page_table + (PDE_P or PDE_W)
|
2024-07-05 08:38:19 +00:00
|
|
|
|
|
|
|
mov eax, boot_page_directory
|
|
|
|
mov cr3, eax
|
|
|
|
|
|
|
|
mov eax, cr0
|
2024-07-08 17:10:37 +00:00
|
|
|
or eax, (CR0_PG or CR0_WP)
|
2024-07-05 08:38:19 +00:00
|
|
|
mov cr0, eax
|
2024-06-14 03:32:46 +00:00
|
|
|
|
2024-07-05 09:10:26 +00:00
|
|
|
mov eax, STPDBOOT_MAGIC
|
|
|
|
mov ebx, boot_structure
|
2024-04-28 06:41:36 +00:00
|
|
|
|
2024-07-08 16:48:31 +00:00
|
|
|
mov ecx, 0xC0000000 + KERNEL_BASE
|
2024-07-05 12:23:58 +00:00
|
|
|
jmp ecx
|
|
|
|
|
2024-03-31 10:27:15 +00:00
|
|
|
hang:
|
|
|
|
hlt
|
|
|
|
jmp $
|
|
|
|
|
2024-03-20 09:48:47 +00:00
|
|
|
_edata:
|
|
|
|
|
2024-07-02 08:32:11 +00:00
|
|
|
boot_structure BootInfo
|
2024-04-02 10:03:47 +00:00
|
|
|
|
2024-03-31 10:27:15 +00:00
|
|
|
align 4096
|
|
|
|
boot_page_directory:
|
|
|
|
rb 4096
|
|
|
|
|
2024-07-05 05:46:04 +00:00
|
|
|
boot_0_page_table:
|
|
|
|
rb 4096
|
|
|
|
|
2024-03-20 09:48:47 +00:00
|
|
|
_end:
|