home Back

bootloader.asm stage2.asm kernel-entry.asm

Bootloader

_start

saves the drive ID it was booted from into variable BOOT_DRIVE, sets the stack to 0x9000
and sets ES & DS to 0 so that memory operations act from 0x0000 as the base address.

calls load_stage2 to load stage2 of the bootloader...

moves BOOT_DRIVE into AX so stage2 can read AX to get the drive ID, then jumps to stage2 code and starts executing it.


load_stage2

sets BX to the memory address of where to load stage2,
puts the number of sectors to read on the stack for error checks later.

sets up registers to call BIOS 0x13 to load stage2 into memory, then calls BIOS.

checks the carry flag to see if the BIOS called failed, if so, prints an error message and halts.

compares the number on the stack with AL, if they are not equal, then there was an error, prints an error message and halts.

returns if no checks failed.


Small helpers

print_string: uses BIOS calls to print a string.


Bootloader-Stage2

_start

saves the drive ID AX into variable BOOT_DRIVE

calls load_kernel to load... kernel...

writes kernel start address into memory at 0x07FA as 4Bytes
writes the sector count of the kernel to memory at 0x07F8 as 2Bytes

disables interrupts,
calls scan_hardware,
loads the GDT,
and sets the protected mode bit in CR0.

its now in 32bit mode.

far jumps to mode32b_start.


load_kernel

DAP

size reserved sector-to-read offset segment LBA
1B 1B 2B 2B 2B 8B
sets SI to DAP's address.

loads the address of where to load the kernel into DAP.offset and DAP.segment,
loads the LBA address of where the kernel is on disk into DAP.LBA.

sets CX to the number of sectors to load.

calls BIOS to do an "extended read" which loads the kernel into memory, checks carry flag, if set, prints an error message and halts.

adds 512 to DAP.offset, if carry flag set, adds 0x1000 to DAP.segment.

decerments CX by 1 and if not zero, else loops back to read more sectors.

returns back to _start


scan_hardware

calls scan_ram


scan_ram

sets DX to the start of e820's entry buffer,
and clears e820's entry count.

calls BIOS to get system memory map entry.

checks if EAX equals "SMAP" aka 0x534D4150, if not equal, return,
checks if carry set, if so, return,
if EBX zero, then return.

incerment e820's entry count by 1,
add 24 to DX as not to overwrite the previous e820 entry.

jumps back to read the next entry.


GDT

i dont remember how it works


mode32b_start

clears all registers with a 32 bit value to reset the CPU cache.

sets stack to 0x90000.

cld is vary much so required.

calls the kernel code.

halts if the kernel ever returns.


Small helpers

print_string: uses BIOS calls to print a string.


Kernel-Entry

used to allow the linker to connect the bootloader and kernel