Hey I have a problem when loading kernel, bootloader and protected mode works fine but when I try to call KERNEL_OFFSET
there are glitches or loading problems and I can’t figure it out.
Problem with Booting
source/boot.s
bits 16
org 0x7c00
KERNEL_OFFSET equ 0x1000
VIDEO_MEMORY equ 0xb8000
boot:
mov ah, 0x0
mov al, 0x3
int 0x10
mov [BOOT_DRIVE], dl
mov bp, 0x9000
mov sp, bp
mov bx, KERNEL_OFFSET
mov dh, 30
mov dl, [BOOT_DRIVE]
mov ah, 0x02
mov al, 0x04
mov cl, 0x02
mov ch, 0x00
int 0x13
;Enable A20
in al, 0x64
test al, 2
jnz $
mov al, 0xD1
out 0x64, al
in al, 0x64
test al, 2
jnz $
mov al, 0xDF
out 0x60, al
lgdt [GDT_DESCRIPTOR]
mov eax, cr0
or eax, 0x1
mov cr0, eax
jmp CODE_SEGMENT:protected_mode
GDT_START:
dq 0x0
GDT_CODE:
dw 0xFFFF
dw 0x0
db 0x0
db 10011010b
db 11001111b
db 0x0
GDT_DATA:
dw 0xFFFF
dw 0x0
db 0x0
db 10010010b
db 11001111b
db 0x0
GDT_END:
GDT_DESCRIPTOR:
dw GDT_END - GDT_START
dd GDT_START
CODE_SEGMENT equ GDT_CODE - GDT_START
DATA_SEGMENT equ GDT_DATA - GDT_START
bits 32
protected_mode:
cli
cld
mov ax, DATA_SEGMENT
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
mov ebp, 0x9FB00
mov esp, ebp
mov edx, VIDEO_MEMORY
mov ebx, MESSAGE
call print
; call KERNEL_OFFSET
jmp $
print:
mov al, [ebx]
mov ah, 0x0F
cmp al, 0
jz .done
mov word [edx], ax
add edx, 2
add ebx, 1
jmp print
.done:
ret
halt:
cli
hlt
BOOT_DRIVE: db 0
MESSAGE: db "Hello, World!", 0
times 510 - ($ - $$) db 0
dw 0xAA55
source/kernel.c
void kernel_main() {
char* VGA_MEMORY = (char*) 0xb8000;
*VGA_MEMORY = 'X';
while (1);
}
source/linker.ld
ENTRY(kernel_main)
SECTIONS {
. = 0x1000;
.text : {
*(.text)
}
.rodata : {
*(.rodata)
}
.data : {
*(.data)
}
.bss : {
bss_start = .;
*(.bss)
bss_end = .;
}
}
makefile
all:
mkdir -p build
nasm -f bin source/boot.s -o build/boot.bin
gcc -ffreestanding -fno-builtin -fno-pic -m32 -c source/kernel.c -o build/kernel.o
ld -m elf_i386 -T source/linker.ld -o build/kernel.bin --oformat binary build/kernel.o
dd if=/dev/zero of=image.bin bs=512 count=2880
dd if=build/boot.bin of=build/os.bin conv=notrunc
dd if=build/kernel.bin of=build/os.bin conv=notrunc bs=512 seek=1
rm build/kernel.o build/kernel.bin build/boot.bin
run:
qemu-system-x86_64 -drive file=build/os.bin,format=raw,media=disk
I’m looking for a solve to begin loading the kernel. :>
New contributor
Machina is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.