172 lines
4.2 KiB
NASM
172 lines
4.2 KiB
NASM
|
|
;*******************************************************
|
|
;
|
|
; Stage2.asm
|
|
; Stage2 Bootloader
|
|
;
|
|
; OS Development Series
|
|
;*******************************************************
|
|
|
|
bits 16
|
|
|
|
org 0x500
|
|
|
|
jmp main ; go to start
|
|
|
|
;*******************************************************
|
|
; Preprocessor directives
|
|
;*******************************************************
|
|
|
|
%include "stdio.inc" ; basic i/o routines
|
|
%include "gdt.inc" ; Gdt routines
|
|
%include "a20.inc" ; A20 enabling
|
|
%include "fat12.inc" ; FAT12 driver. Kinda :)
|
|
%include "common.inc"
|
|
;%include "bootinfo.inc"
|
|
%include "memory.inc"
|
|
|
|
;*******************************************************
|
|
; Data Section
|
|
;*******************************************************
|
|
|
|
msgFailure db 0x0D, 0x0A, "FATAL ERROR: Kernel file KERNEL.CTA missing or corrupt. Press Any Key to Reboot.", 0x0D, 0x0A, 0x0A, 0x00
|
|
|
|
boot_info:
|
|
multiboot_info_flags dd 0
|
|
multiboot_info_memoryLo dd 0
|
|
multiboot_info_memoryHi dd 0
|
|
multiboot_info_bootDevice dd 0
|
|
multiboot_info_cmdLine dd 0
|
|
multiboot_info_mods_count dd 0
|
|
multiboot_info_mods_addr dd 0
|
|
multiboot_info_syms0 dd 0
|
|
multiboot_info_syms1 dd 0
|
|
multiboot_info_syms2 dd 0
|
|
multiboot_info_mmap_length dd 0
|
|
multiboot_info_mmap_addr dd 0
|
|
multiboot_info_drives_length dd 0
|
|
multiboot_info_drives_addr dd 0
|
|
multiboot_info_config_table dd 0
|
|
multiboot_info_bootloader_name dd 0
|
|
multiboot_info_apm_table dd 0
|
|
multiboot_info_vbe_control_info dd 0
|
|
multiboot_info_vbe_mode_info dw 0
|
|
multiboot_info_vbe_interface_seg dw 0
|
|
multiboot_info_vbe_interface_off dw 0
|
|
multiboot_info_vbe_interface_len dw 0
|
|
|
|
main:
|
|
|
|
;-------------------------------;
|
|
; Setup segments and stack ;
|
|
;-------------------------------;
|
|
|
|
cli ; clear interrupts
|
|
xor ax, ax ; null segments
|
|
mov ds, ax
|
|
mov es, ax
|
|
mov ax, 0x0000 ; stack begins at 0x9000-0xffff
|
|
mov ss, ax
|
|
mov sp, 0xFFFF
|
|
sti ; enable interrupts
|
|
|
|
mov [multiboot_info_bootDevice], dl
|
|
|
|
call _EnableA20
|
|
call InstallGDT
|
|
sti
|
|
|
|
xor eax, eax
|
|
xor ebx, ebx
|
|
call BiosGetMemorySize64MB
|
|
|
|
mov word [multiboot_info_memoryHi], bx
|
|
mov word [multiboot_info_memoryLo], ax
|
|
|
|
mov eax, 0x0
|
|
mov ds, ax
|
|
mov di, 0x1000
|
|
call BiosGetMemoryMap
|
|
mov dword [multiboot_info_mmap_addr], 0x1000
|
|
xor eax, eax
|
|
mov ax, bp
|
|
mov dword [multiboot_info_mmap_length], eax
|
|
|
|
|
|
call LoadRoot
|
|
mov ebx, 0
|
|
mov ebp, IMAGE_RMODE_BASE
|
|
mov esi, ImageName
|
|
call LoadFile ; load our file
|
|
mov dword [ImageSize], ecx
|
|
cmp ax, 0
|
|
je EnterStage3
|
|
mov si, msgFailure
|
|
call Puts16
|
|
mov ah, 0
|
|
int 0x16 ; await keypress
|
|
int 0x19 ; warm boot computer
|
|
|
|
;-------------------------------;
|
|
; Go into pmode ;
|
|
;-------------------------------;
|
|
|
|
EnterStage3:
|
|
|
|
cli ; clear interrupts
|
|
mov eax, cr0 ; set bit 0 in cr0--enter pmode
|
|
or eax, 1
|
|
mov cr0, eax
|
|
|
|
jmp CODE_DESC:Stage3 ; far jump to fix CS. Remember that the code selector is 0x8!
|
|
|
|
; Note: Do NOT re-enable interrupts! Doing so will triple fault!
|
|
; We will fix this in Stage 3.
|
|
|
|
;******************************************************
|
|
; ENTRY POINT FOR STAGE 3
|
|
;******************************************************
|
|
|
|
bits 32
|
|
|
|
BadImage db "FATAL ERROR: Kernel file KERNEL.CTA missing or corrupt. Press Any Key to Reboot.", 0
|
|
|
|
Stage3:
|
|
|
|
;-------------------------------;
|
|
; Set registers ;
|
|
;-------------------------------;
|
|
|
|
mov ax, DATA_DESC ; set data segments to data selector (0x10)
|
|
mov ds, ax
|
|
mov ss, ax
|
|
mov es, ax
|
|
mov esp, 90000h ; stack begins from 90000h
|
|
|
|
|
|
CopyImage:
|
|
mov eax, dword [ImageSize]
|
|
movzx ebx, word [bpbBytesPerSector]
|
|
mul ebx
|
|
mov ebx, 4
|
|
div ebx
|
|
cld
|
|
mov esi, IMAGE_RMODE_BASE
|
|
mov edi, IMAGE_PMODE_BASE
|
|
mov ecx, eax
|
|
rep movsd ; copy image to its protected mode address
|
|
|
|
mov eax, 0x2badb002 ; multiboot specs say eax should be this
|
|
mov ebx, 0
|
|
|
|
;edx=8
|
|
|
|
push dword boot_info
|
|
push dword [ImageSize]
|
|
|
|
jmp CODE_DESC:IMAGE_PMODE_BASE ; Execute Kernel
|
|
add esp, 4
|
|
|
|
cli
|
|
hlt
|
|
|