Page 1 of 1
booting C kernel
Posted: Thu Oct 31, 2002 12:00 am
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
RE:booting C kernel
Posted: Fri Nov 01, 2002 12:00 am
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
RE:booting C kernel
Posted: Sat Nov 02, 2002 12:00 am
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.