I am trying to create a bootloader and later on a kernel in c.
The project consists of four files:
bootloader.s
kernel_start.s
link.ld
Makefile
bootloader.s obviously contains the bootloader code.
kernel_start.s is the code that bootloader will jump to.
link.ld is the linker script.
Before i tell you the "problem" let me post two of the files above:
bootloader.s
(swedish comments removed)
Code: Select all
[BITS 16]
%define SECTORS_TO_LOAD 1
jmp 7C0h:bootloader_entry
bootloader_entry:
mov ax, 7C0h
mov ds, ax
mov es, ax
mov ss, ax
mov sp, stack
call load_kernel
call enter_protected_mode
jmp hang
load_kernel:
mov ax, 7C0h
mov es, ax
mov bx, 200h
mov al, SECTORS_TO_LOAD
mov ch, 00h
mov cl, 02h
mov dh, 00h
mov dl, 00h
mov ah, 02h
int 13h
jc error_loading_kernel
cmp ah, 00h
jne error_loading_kernel
cmp al, SECTORS_TO_LOAD
jne error_loading_kernel
cmp word [200h], "TK"
jne error_loading_kernel
mov si, msg_kernel_loaded
call puts
ret
error_loading_kernel:
mov si, msg_error_loading_kernel
call puts
jmp hang
enter_protected_mode:
cli
lgdt [temp_gdt_desc]
mov eax, cr0
or eax, 1
mov cr0, eax
jmp 08h:pmode
pmode:
mov ax, 10h
mov ds, ax
mov es, ax
mov ss, ax
mov si, msg_protected_mode
call puts
ret
puts:
lodsb
cmp al, 00h
je return
mov ah, 0Eh
int 10h
jmp puts
wait_for_any_key:
mov ah, 00h
int 16h
return:
ret
hang:
jmp hang
msg_kernel_loaded db "Successfully loaded kernel from floppy disk.", 00h
msg_error_loading_kernel db "Couldn't load kernel from floppy disk.", 00h
msg_protected_mode db "Entered protected mode."
msg_press_any_key db "Press any key to jump to the kernel...", 00h
temp_gdt_desc:
dw end_temp_gdt - temp_gdt - 1
dd temp_gdt
temp_gdt:
; Null descriptor
dd 0
dd 0
; Code segment
dw 0FFFFh
dw 7C00h
db 0
db 10011010b
db 11001111b
db 0
; Data segment
dw 0FFFFh
dw 7C00h
db 0
db 10010010b
db 11001111b
db 0
end_temp_gdt:
times 510-($-$$) db 0
dw 0AA55h
SECTION .bss
resb 8192
stack:
(I'm not sure if the SECTIONS part is correct. Should I specify there that the bootloader is loaded to 0x7C00?)
Code: Select all
OUTPUT_FORMAT("binary")
SECTIONS
{
.text :
{
*(.text)
. = ALIGN(4096);
}
.data :
{
*(.data)
*.(rodata)
. = ALIGN(4096);
}
.bss :
{
*(.bss)
. = ALIGN(4096);
}
}
I am not sure about this part. How can this code use the eax register when the computer is in Real Mode? Please explain
Code: Select all
mov eax, cr0
or eax, 1
mov cr0, eax
Please help me to enter Protected Mode!
tobbebia