luxos/SysBoot/stage2/stage2.asm

263 lines
7.7 KiB
NASM

;***** stage2.asm ****************************************************
;* (c) 2010 CTA Systems Inc. All rights reserved. Glory To God *
;* *
;* Stage 2 Bootloader *
;* ================== *
;* *
;************************************************************ cta os */
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"
%include "getdata.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:
; Memory
multiboot_info_memoryLo dd 0
multiboot_info_memoryHi dd 0
; Boot device
multiboot_info_bootDevice dd 0
; Pointer to a char[] string. (not implemented)
multiboot_info_cmdLine dd 0
; Other modules loaded by bootloader. (not implemented
multiboot_info_mods_count dd 0
multiboot_info_mods_addr dd 0
; Pointer to memory map
multiboot_info_mmap_length dd 0
multiboot_info_mmap_addr dd 0
; TODO:
multiboot_info_drives_length dd 0
multiboot_info_drives_addr dd 0
; BIOS ROM config table. TODO:
multiboot_info_config_table dd 0
; CTA bootloader name
multiboot_info_bootloader_name db "CTA", 0
; TODO:
multiboot_info_apm_table dd 0
; Returns VGA current video mode info
multiboot_info_video_mode db 0
multiboot_info_video_columns db 0
multiboot_info_video_page db 0
; Returns VESA information
multiboot_info_vbe_control_info dd 0
multiboot_info_vbe_mode_info dd 0
multiboot_info_vbe_mode dw 0
multiboot_info_vbe_interface_seg dw 0
multiboot_info_vbe_interface_off dw 0
multiboot_info_vbe_interface_len dw 0
vbeControllerInfo:
vbeControllerInfo_signature dd 0 ; "VESA", taken as 4 bytes
vbeControllerInfo_version dw 0 ; 0x0300 for VBE 3.0
vbeControllerInfo_oemString dd 0 ; isa vbeFarPtr, taken as 2 shorts
vbeControllerInfo_capabilities dd 0 ; taken as 4 bytes
vbeControllerInfo_videomodes dd 0 ; isa vbeFarPtr, taken as 2 shorts
vbeControllerInfo_totalMemory dw 0 ; as # of 64k blocks
TemporaryStorage dd 0 ; temporary storage
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
call _EnableA20
call InstallGDT
sti
;-------------------------------;
; Fill in the boot structure ;
;-------------------------------;
; Memory Size
xor eax, eax
xor ebx, ebx
call BiosGetMemorySize64MB
push eax
mov eax, 64
mul ebx
mov ecx, eax
pop eax
add eax, ecx
add eax, 1024 ; the routine doesnt add the KB between 0-1MB; add it
mov dword [multiboot_info_memoryHi], 0
mov dword [multiboot_info_memoryLo], eax
;Memory map
mov eax, 0x0
mov ds, ax
mov di, 0x1000
call BiosGetMemoryMap
mov dword [multiboot_info_mmap_addr], 0x1000 ; address
xor eax, eax
mov ax, bp
mov dword [multiboot_info_mmap_length], eax ; length
call gatherinfo
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
%include "paging.inc"
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
call EnablePaging
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, 0xC0DEcC7A ; cta bootloader specific
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
vbeModeInfo:
vbeModeInfo_attributes dw 0
vbeModeInfo_winA db 0
vbeModeInfo_winB db 0
vbeModeInfo_granularity dw 0
vbeModeInfo_winsize dw 0
vbeModeInfo_segmentA dw 0
vbeModeInfo_segmentB dw 0
vbeModeInfo_realFctPtr dd 0
vbeModeInfo_pitc dw 0 ; // bytes per scanline
vbeModeInfo_Xres dw 0
vbeModeInfo_Yres dw 0
vbeModeInfo_Wchar db 0
vbeModeInfo_Ychar db 0
vbeModeInfo_planes db 0
vbeModeInfo_bpp db 0
vbeModeInfo_banks db 0
vbeModeInfo_memory_model db 0
vbeModeInfo_bank_size db 0
vbeModeInfo_image_pages db 0
vbeModeInfo_reserved0 db 0
; VBE v1.2+
vbeModeInfo_red_mask db 0
vbeModeInfo_red_position db 0
vbeModeInfo_green_mask db 0
vbeModeInfo_green_position db 0
vbeModeInfo_blue_mask db 0
vbeModeInfo_blue_position db 0
vbeModeInfo_rsv_mask db 0
vbeModeInfo_rsv_position db 0
vbeModeInfo_directcolor_attrib db 0
; VBE v2.0+
vbeModeInfo_physbase dd 0
vbeModeInfo_start_offscreen_mem dd 0
vbeModeInfo_size_offscreen_mem dw 0
; VBE v3.0+
vbeModeInfo_bytes_per_scanline dw 0
vbeModeInfo_number_images_banked db 0
vbeModeInfo_number_images_linear db 0
vbeModeInfo_linear_red_mask db 0
vbeModeInfo_linear_red_pos db 0
vbeModeInfo_linear_green_mask db 0
vbeModeInfo_linear_green_pos db 0
vbeModeInfo_linear_blue_mask db 0
vbeModeInfo_linear_blue_pos db 0
vbeModeInfo_linear_res_mask db 0
vbeModeInfo_linear_res_pos db 0
vbeModeInfo_max_pixel_clock dd 0