Using C functions in bootloader

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
deph
Posts: 13
Joined: Sun Jun 22, 2008 6:04 am

Using C functions in bootloader

Post 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;
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Re: Using C functions in bootloader

Post 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...
Post Reply