booting C kernel

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
xtremist

booting C kernel

Post by xtremist »

hi..
im a newbie os-developer, and i was able to write a bootsector which loads
sector 2 into a specified address and starts executing it...sector 2 contains asm code..
i would like to know how i can write C code and get the bootsector to jump to it

thanks.
pratap
Keith

RE:booting C kernel

Post by Keith »

Pretty much the same way.  Though how you go about doing this, would be up to you.  You could have your 'C code' which I'll now call 'kernel' in binary format, then it's a matter of just loading it up and executing it.  Depending on your file system, your bootsector might have to figure out some of that...if the kernel is in a binary format (like ELF), you'd need to move that around in memory a bit, etc...  but for the most part, it's the same principle...you'll just end up reading more than 1 sector :)
Schol-R-LEA

RE:booting C kernel

Post by Schol-R-LEA »

A few points to be made:

First, if you are using a 32-bit C compiler such as gcc, you will need to switch into protected mode before running the C code. If you already have done this in your bootloader, all is good; if not, it may be a lot easier to use a second-stage loader in assembly which switches to p-mode and then loads the kernel image.

Second, you'll probably want to use a small assembly language 'stub' at the beginning of the second stage, which is linked to the kernel code and calls it directly. This allows you to place the entry point where you need it, otherwise it will be rather difficult. Conversely, you can write a second stage loader, which can read an ELF executable from the file system and load it in the conventional manner.

Third, while I agree that writing your own boot loader is a good and insightful  programming exercise, I don't recommend it in a final design. Existing boot loader such as GRUB and BING have been written which already handle these issues, and which are designed to cover many more potentialities than a simple OS sepcifc loader would (including booting multiple OSes from a single disk, in the case of GRUB). While it is only a suggestion, it is one which is certain to save you a lot of time and effort.
Post Reply