;******************************************************* ; ; Stage2.asm ; Stage2 Bootloader ; ; OS Development Series ;******************************************************* bits 16 ; Remember the memory map-- 0x500 through 0x7bff is unused above the BIOS data area. ; We are loaded at 0x500 (0x50:0) 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" ;******************************************************* ; Data Section ;******************************************************* msgFailure db 0x0A, 0x0D, "FATAL ERROR: MISSING OR CORRUPT KERNEL.CTA", 0x0A, 0x0D, "Press any key to reboot...", 0x0D, 0x0A, 0x00 ;******************************************************* ; STAGE 2 ENTRY POINT ; ; -Store BIOS information ; -Load Kernel ; -Install GDT; go into protected mode (pmode) ; -Jump to Stage 3 ;******************************************************* main: ;-------------------------------; ; Setup segments and stack ; ;-------------------------------; cli ; clear interrupts xor ax, ax ; null segments mov ds, ax mov es, ax mov ax, 0x0 ; stack begins at 0x9000-0xffff mov ss, ax mov sp, 0xFFFF sti ; enable interrupts ;-------------------------------; ; Install our GDT ; ;-------------------------------; call InstallGDT ; install our GDT ;-------------------------------; ; Enable A20 ; ;-------------------------------; call EnableA20_KKbrd_Out ;-------------------------------; ; Initialize filesystem ; ;-------------------------------; call LoadRoot ; Load root directory table ;-------------------------------; ; Load Kernel ; ;-------------------------------; mov ebx, 0 ; BX:BP points to buffer to load to mov bp, IMAGE_RMODE_BASE mov si, ImageName ; our file to load call LoadFile ; load our file mov dword [ImageSize], ecx ; save size of kernel cmp ax, 0 ; Test for success je EnterStage3 ; yep--onto Stage 3! mov si, msgFailure ; Nope--print error call Puts16 mov ah, 0 int 0x16 ; await keypress int 0x19 ; warm boot computer cli ; If we get here, something really went wong hlt ;-------------------------------; ; 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 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 ;-------------------------------; ; Copy kernel to 1MB ; ;-------------------------------; 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 ;---------------------------------------; ; Execute Kernel ; ;---------------------------------------; jmp CODE_DESC:IMAGE_PMODE_BASE; jump to our kernel! Note: This assumes Kernel's entry point is at 1 MB ;---------------------------------------; ; Stop execution ; ;---------------------------------------; cli hlt