Search code examples
ckernelcross-compilingbootloaderfasm

Cross-compiling c kernel from x64 system to x32 binary


I'm trying to create an OS kernel in educational purposes.
Using this guide I've written a bootloader in fasm:

use16
org 0x7c00

start:
    jmp kernel_start

KERNEL_OFFSET equ 0x1000


gdt_start:

gdt_null:
    dd 0x0
    dd 0x0

gdt_code:
    dw 0xffff
    dw 0x0000
    db 0x00
    db 10011010b
    db 11001111b
    db 0x00

gdt_data:
    dw 0xffff
    dw 0x0000
    db 0x00
    db 10010010b
    db 11001111b
    db 0x00

gdt_end:

gdt_descriptor:
    dw gdt_end - gdt_start - 1
    dd gdt_start



CODE_SEG equ gdt_code - gdt_start
DATA_SEG equ gdt_data - gdt_start



read_disk:
    pusha
    push dx

    mov ah, 0x02
    mov al, dh
    mov ch, 0x00
    mov dh, 0x00
    mov cl, 0x02

    int 0x13

    jc .disk_error

    pop dx
    cmp dh, al
    jne .disk_error
    jmp .done

.disk_error:
    mov bx, ERROR_MSG
    call write_string
    stc

.done:
    popa
    ret


write_string:
    pusha
    mov ah, 0x0e

.repeat:
    lodsb
    cmp al, 0x00
    je .done
    int 0x10
    jmp .repeat

.done:
    popa
    ret



kernel_start:
    mov [BOOT_DRIVE], dl

    cli
    mov ax, cs
    mov ss, ax
    mov sp, start
    mov bp, start
    sti

    mov ds, ax
    mov es, ax

    mov si, BOOT_MSG
    call write_string

    mov dh, 15
    mov dl, [BOOT_DRIVE]
    mov bx, KERNEL_OFFSET
    call read_disk

    cli
    lgdt [gdt_descriptor]
    mov eax, cr0
    or eax, 0x01
    mov cr0, eax

    jmp CODE_SEG:kernel_launch



use32

kernel_launch:

    mov ax, DATA_SEG
    mov ds, ax
    mov ss, ax
    mov es, ax
    mov fs, ax
    mov gs, ax

    mov ebp, start
    mov esp, start

    call KERNEL_OFFSET

    jmp $



variables:
    BOOT_DRIVE db 0
    KERNEL_LEN file "kernel_info.bb": 0x0, 1
    BOOT_MSG   db "Booted in real mode...", 0x0d, 0x0a, 0
    ERROR_MSG  db "Could not load second boot loader!", 0x0d, 0x0a, 0

magic_numbers:
    times 510-($-$$) db 0
    dw 0xaa55

I've compiled it with fasm loader.asm and made an iso using dd. It works fine printing message in OracleVM.

And now it's time to combine it with c part. But the problem is: I can not build on target (x32) device. I'm forced to use x64.
According to this manual I've found a way to compile elf executables and link them with multiboot. But I don't want to use multiboot standart - I'd rather prefer to use a self-written bootloader (the one above).

Which tools, arguments and configurations should I use to compile c code to x32, link it with assembler part and create a binary?
Or maybe I should compile assempler-part binary and c-part separately and then write them to disk image one after another?

So far I couldn't find any answer in internet. Is it possible to bring these two kernel parts together at all?

Here is my c code:

void main() {
    char* video_memory = 0xb8000;
    *video_memory = ’X’;
}

Update: I've tried the following comands:

$nasm loader.asm -f bin -o loader.bin
$gcc -m32 -fno-pie -ffreestanding -c kernel.c -o kernel.o
$ld -m -elf_i386 -o kernel.bin -Ttext 0x1000 kernel.o --oformat binary
$cat loader.bin kernel.bin > os-image

to compile bootloader and kernel as binaries and then write them to disk image. Bootloader works as expected, but no message from kernel is shown.


Solution

  • I found a bug (in case someone will meet the same problem), there was a problem with building script, I should've used:

    $ gcc -m32 -ffreestanding -c kernel.c -o kernel.o
    

    Instead of:

    $ gcc -m32 -fno-pie -ffreestanding -c kernel.c -o kernel.o
    

    (no need in -fno-pie flag).

    Also the command:

    $ ld -m -elf_i386 -o kernel.bin -Ttext 0x1000 kernel.o --oformat binary
    

    Should've been splitted in two parts:

    $ ld -m -elf_i386 -o kernel.elf -Ttext 0x1000 kernel.o
    $ objcopy -O binary kernel.elf kernel.bin
    

    And that's it!