liaquore
liaquore

Reputation: 397

How to properly create a kernel in C and link to bootloader

I have a simple bootloader in assembly here:

;initialization type stuff
[bits 16]
[org 0x7c00]

;make message and print it
mov si, MESSAGE
call print

;infinite loop to stall
jmp $

;print "method"
print:
    pusha
    print_loop:
        mov al, [si]
        cmp al, 0
        jne print_char
        popa
        ret
    print_char:
        mov ah, 0x0e
        int 0x10
        add si, 1
        jmp print_loop

;actual assigning of message
MESSAGE: db "Hello World!", 0

;padding & magic number (end)
times 510-($-$$) db 0
dw 0xaa55

And my goal is to have most of the OS programmed in C, and at the moment I'm trying to properly link the bootloader and kernel in C:

void kernel_main()
{
    printf("Hello World!");
}

How would I link these together and compile them into something I can open in qemu/any normal computer?

Upvotes: 2

Views: 1147

Answers (1)

Brendan
Brendan

Reputation: 37214

How to properly create a kernel in C and link to bootloader

Don't.

An OS is typically many separate executable files (boot loader, kernel, GUI, text editor, web browser, ...) that are not linked together into a single massive file that does everything (e.g. "Ubuntu.exe").

For boot loaders for 80x86; in general you may have:

  • a boot loader for BIOS designed for unpartitioned storage devices (e.g. ancient floppy disk)
  • a boot loader for BIOS designed for "MBR partitioned" storage devices
  • a boot loader for BIOS designed for "GPT partitioned" storage devices
  • a boot loader for BIOS designed for bootable CDs ("no emulation El Torito")
  • a boot loader for BIOS designed for PXE/network boot
  • a boot loader designed UEFI (and compiled for 32-bit)
  • a boot loader designed UEFI (and compiled for 64-bit)

Each of these things involves some very different code (boot loader file formats, firmware restrictions, firmware APIs, where to get kernel from, etc).

You want to ensure that any of your boot loaders can work with the same kernel (and ensure that one specific boot loader that might be the wrong one hasn't been built into/linked with the kernel).

Don't forget that (eventually, not during early development) end users download/obtain some kind of "OS installer"; and the "OS installer" determines the correct boot loader to install with the OS (likely after asking the user if/where/how they want the OS to be installed and after detecting various pieces of information about the computer the OS is being installed on - what type of CPU, what type of firmware, what type of partitioning scheme, ...). You can't know which boot loader is the right boot loader until the "OS installer" has done a lot of work (and can't know which boot loader is the right boot loader when building your project).

Upvotes: 4

Related Questions