Page 1 of 1

Using C functions in bootloader

Posted: Thu Jun 26, 2008 8:36 am
by deph
Cand anyone please explain (or give a tutorial on) how to use C written code in a bootlaoder (just like GRub does <however grub code is very hard to follow> ).

I want to write a two-stage bootloader. First stage loads stage2 from the next "N" sectors (where N is known). I want to use both assembly and C in stage2. I would like to implement support for FS in C (since implementing it in ASM is a real pain). I might want to implement some functions in ASM which I could use in C, like something to enter and leave pmode (in order to use BIOS HDD <since I am not planning to implement support for IDE and SCSI in the BLoader !>)

I am using the Gnu C Compiler;

Re: Using C functions in bootloader

Posted: Thu Jun 26, 2008 9:41 am
by suthers
If your still in real mode the first step will probably be to get your compiler to output 16bit code.
The just compile your code C code using the wright command line options so it doesn't use any OS specific code, calls, etc...
I use this with GCC: -Wall -O -fstrength-reduce -fomit-frame-pointer -fno-builtin -c
But you can add other things depending on your preferences... (See gcc manual).
The you define whatever C functions as extern in your assembly code (and vise-versa...)
and link them... (I use LD and you'll be needing to write a linker script....)
You get the linker to output a flat binary (defined in the linker script.)
The you put the generated binary at the beginning of an image formated for whatever media you want to use...
Jules

edit: for externs, you have to get your assembler to output in a non flat binary format (in a format that supports externs basically...), I use elf...